简体   繁体   中英

htaccess redirect url from file to root dir

I've been trying to crack this for a few hours now and now reaching out for an answer/steer if possible.

文件结构

Trying to go from

www.domain.co.uk/Product.php?product=solid-oak-dining-table

to

www.domain.co.uk/solid-oak-dining-table/

I tried a lot of Google/other code but the closest I've found is...

RewriteRule ^([a-zA-Z0-9]+)$ Product.php?product=$1
RewriteRule ^([a-zA-Z0-9]+)/$ Product.php?product=$1

but I just get a 404. Happy to change file locations or setup but the end result has to be the product name on the root.

Thanks so much.

I think the issue is you are not accounting for the - in your query string so you need to change this and from your screenshot it seems you are using windows so you need to enable mod_rewrite apache module if it is not already enabled and put this in your .htaccess

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /

RewriteRule ^([a-zA-Z0-9]+)$ Product.php?product=$1
</IfModule>

And if you are using xampp or wamp and files are in a folder called for example test your htaccess file inside that folder needs to look something like:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /test/

RewriteRule ^([a-zA-Z0-9]+)$ Product.php?product=$1
</IfModule>

Check this redirect with query string

<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} ^Product\.php
RewriteCond %{QUERY_STRING} product=([A-Za-z0-9-]+) [NC]
RewriteRule (.*) /$1/ [R=301,L]
</IfModule>

Did a Wordpress install on a dev server to generate an .htacces file

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^Product\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /Product.php [L]
</IfModule>

Works a treat! Hope this helps out someone else. 7 Hours to get a short url!

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