Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

multiple URL rewrites in same .htaccess

I currently have a site that contains individual pages for a companies stores as well as standard pages such as about, contact etc.

I have a simple URL rewrite rule in the htaccess file so that these urls are written as

https://sitename.com/manchester or https://sitename.com/about instead of https://sitename.com/manchester.php and https://sitename.com/about.php etc

As its growing I'm now moving to a database structure for the stores, so instead of creating individual pages for each store I'm serving them up in a query with a select_store.php file ie https://sitename.com/select_store.php?store=manchester etc

I want to continue however rewriting the urls as before so still just /manchester etc but how does this work when I have some URLs that should redirect to select_store.php and others that should simply add .php onto the end of the url?

What's the best way to do this? Do I write individual rewrites for the standard pages? about, contact etc? and then a catch all for anything else to go the select_store.php?

Or do I redirect everything to select_store and then redirect the likes of about, contact in there? (doesn't sound very SEO friendly though).

My current .htaccess is:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [NC,L]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

edited to add additional examples.

currently all pages are suffixed with .php so we have the following: https://sitename.com/manchester redirects to https://sitename.com/manchester.php https://sitename.com/liverpool redirects to https://sitename.com/liverpool.php https://sitename.com/about redirects to https://sitename.com/about.php

I want the stores (in above example manchester & liverpool, but in reality theres a dozen and more adding regularly) to redirect to the likes of: https://sitename.com/select_shop.php?shop=manchester

while the standard pages (about, contact, menu etc) to continue to redirect as: https://sitename.com/about.php

https://sitename.com/contact.php

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So the answer to this turned out to be far simpler than I assumed.

You have a single line for each of the "standard" pages index, about, menu etc, for example

RewriteRule ^about$ about.php [NC,L]

telling them to redirect to the .php file and then have the code to pass anything else to the select_shop page

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ select_shop.php?shop=$1 [NC,L]

so the complete file would be something like:

RewriteEngine On
RewriteRule ^about$ about.php [NC,L]
RewriteRule ^menu$ menu.php [NC,L]
RewriteRule ^franchise$ franchise.php [NC,L]
RewriteRule ^meet-the-team$ meet-the-team.php [NC,L]
RewriteRule ^events$ events.php [NC,L]
RewriteRule ^find-contact-us$ find-contact-us.php [NC,L]
RewriteRule ^join-our-team.php$ join-our-team.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ select_shop.php?shop=$1 [NC,L]

RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...