简体   繁体   English

.htaccess 301重定向和查询字符串

[英].htaccess 301 redirect and query string

I'm having trouble getting a regex to work correctly in my htaccess, I need to take the following url: 我无法让正则表达式在htaccess中正常工作,我需要使用以下网址:

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= 我可以将其重定向到以下内容,但不能将其删除?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. 我已经在regexr.com上测试了一些正则表达式,它们在那儿工作正常,但是当我必须将其实际放入.htaccess时,它不起作用。 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中的正则表达式没有那么好。

.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]

You have to check %{QUERY_STRING} to capture ?id=xxx . 您必须检查%{QUERY_STRING}才能捕获?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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM