开发者

Help with .htaccess RewriteRule. Need to take user from one URL to other

开发者 https://www.devze.com 2022-12-14 01:48 出处:网络
I want users who type http://www.example.com/word-of-the-day to be taken to http://www.example.com/index.php?page=word-of-the-day

I want users who type

http://www.example.com/word-of-the-day

to be taken to

http://www.example.com/index.php?page=word-of-the-day

But I want

http://www.example.com/word-of-the-day

to be shown in the URL for the user.

What should I do in my .htaccess? I tried but the regular expression and the syntax of RewriteRule is way too complicated for me to figure out how to do it.

Any help will be appreciated.

Edit:

Also, how can I say this in htaccess -

if they type http://www.example.com/word-of-the-day, take them to http://www.example.com/index.php?page=word-of-the-day

or if they type http://www.example.com/something-else, t开发者_如何学Pythonake them to http://www.example.com/index.php?page=something-else

or else, just take them to the URL they typed.


The condition below checks that index.php is not being requested. If not apply the rule. This will work for any of the scenarios you listed above.

RewriteCond %{QUERY_STRING}  ^!.*[index\.php].*$ [NC]    
RewriteRule ^(.*)$ index.php?page=$1 [L]

In response to your comment about only wanting to do this for a few specific pages, it would look like this(as an alternative to Nils edit):

RewriteCond %{QUERY_STRING}  ^!.*[index\.php].*$ [NC]
RewriteCond %{REQUEST_URI}   ^word-of-the-day$ [OR]  
RewriteCond %{REQUEST_URI}   ^something-else$ [OR]
RewriteCond %{REQUEST_URI}   ^even-something-else$
RewriteRule ^(.*)$ index.php?page=$1 [L]


Try this

RewriteEngine on 
RewriteRule ^word-of-the-day$ index.php?page=word-of-the-day

Or more flexible

RewriteEngine on 
RewriteRule ^(.*)$ index.php?page=$1

Not tested, yet it sould work.

To your edit:

Just define those specific URLs manually:

RewriteEngine on 
RewriteRule ^word-of-the-day$ index.php?page=word-of-the-day
RewriteRule ^word-some-example$ index.php?page=some-example
RewriteRule ^some-other$ index.php?page=some-other
0

精彩评论

暂无评论...
验证码 换一张
取 消