1

I'm having trouble getting a regex to work correctly in my htaccess, I need to take the following url:

http://example.com/p/anything.aspx?id=1

And redirect it to:

http://example.com/-/anything/1

I can get it to redirect to the following just fine, but cant get it to drop the ?id=

http://example.com/-/anything/?id=1

I've tested a few regex's out on regexr.com, and they work fine there, but when I got to actually put it into my .htaccess it doesnt work. Any help you can give would be amazing! I'm probably missing something small and stupid, just not that great with regex's in the htaccess.

.htaccess

RewriteEngine On
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
RewriteRule ^p/([^\.]+)[\?&](\w+)=(\w+) /-/example/$3 [L,R=301]

1 Answer 1

2

You have to check %{QUERY_STRING} to capture ?id=xxx.
Also, i optimized your code, you can now replace your current by this one

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L,QSA]

RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /$1.php [L]

RewriteCond %{QUERY_STRING} ^id=([1-9][0-9]*)$ [NC]
RewriteRule ^p/(.+)\.aspx$ /-/$1/%1? [R=301,L]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.