Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

.htaccess rewrite regex

I'm trying to rewrite my URLs in .htaccess. What I have currently works but I'm trying to compress 2 lines of .htaccess into 1 because I've a feeling it's possible, I just can't get fully there.

Here are the two lines that I currently have:

RewriteRule "^admin/view/.*-([0-9]*)/?(unviewed|viewed)?/page-([0-9]*)" "admin/index.php?company=$2&status=$3&page=$4" [NE]
RewriteRule "^admin/view/.*-([0-9]*)/?(unviewed|viewed)?" "admin/index.php?company=$2&status=$3" [NE]

That matches any of the following:

admin/view/my-company-12
admin/view/my-company-12/unviewed
admin/view/my-company-12/viewed/page-2

Any of the above URLs are valid on my website.
The first link would map to admin/index.php?company=12
The second link would map to admin/index.php?company=12&status=unviewed
The third link would map to admin/index.php?company=12&status=viewed&page=2

But what I'm trying to do is compress my 2 lines of .htaccess into 1 regex query.

Here is the regex I have on regex101.com (with ; as the delimiter so I don't have to escape the / character)

^admin/view/.*-([0-9]*)/?(unviewed|viewed)?/?(?:page)?-?([0-9])?

That line matches:

admin/view/my-company-12
admin/view/my-company-12/viewed

..but not:

admin/view/my-company-12/viewed/page-2

The only part of the URL it pulls out in admin/view/my-company-12/viewed/page-2 is the page number (2)

The parts of the URL I'm interested in pulling out are:

  • company ID (the number after my-company-*) (required)
  • viewed / unviewed (optional)
  • page number (the number after page-*) (optional)

Any help at all is appreciated. Thanks!

Comments