简体   繁体   中英

URL Rewrite rule in .htaccess for Root directory

I am using this rule to rewrite the link

RewriteEngine On
RewriteRule ^(.*)/(.*) show_cv.php?email=$1

It is working fine like if I write this url with last slash

www.mysite.com/letschat_2008@yahoo.com/  ---->  index.php?email=letschat_2008@yahoo.com

But when I remove the last slash from the link www.mysite.com/letschat_2008@yahoo.com/ it shows error 404.

I wish the URL Rewrite rule would work for both with slash and without slash ( / )

www.mysite.com/letschat_2008@yahoo.com/ ----> index.php?email=letschat_2008@yahoo.com
www.mysite.com/letschat_2008@yahoo.com ----> index.php?email=letschat_2008@yahoo.com

Your rules are looping, you need to make sure you are rewriting an email address, and add some conditions so that the rule doesn't get applied if it's accessing an existing resource:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9_\-\@\.]+)/?$ /show_cv.php?email=$1 [L]

然后,您应该使用以下规则:

RewriteRule ^(.*)/? show_cv.php?email=$1

I assume you note these rules in a .htaccess file, not in the server configuration when looking at your description ?

Rethink if you don-t want to put this into the server configuration. Apart from the usage of .htaccess files being harder to debug using rewrite rules in those files is more complex than in the server configuration. This is documented in mod_rewrites docs.

The reason for the behaviour is the different content of REQUEST_URI in both cases. Have a try checking this directly and you will see the problem. The whole part "letschat_2008@yahoo.com" is simply missing in that variable in case 2 (no "/"). To get this working you must use an additional rewriteCondition (also documented...). Something like these untested lines:

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(.+)&
RewriteRule - show_cv.php?email=%1

(note the '%' instead of a '$' in the last line)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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