简体   繁体   中英

.htaccess rewrites for specific directories

I've been assigned to a new website and I need to incorporate page routing and "vanity URLs" with .htaccess

I'm really not familiar with htaccess, and despite all my research efforts, I have not gotten any solid answers.

The website has a custom-built PHP admin system located in a subdirectory called "admin". I don't want to mess with it at all, so whatever changes I made to htaccess cannot affect that subdirectory.

So, given that the page has 3 main "pages": home, buy, and sell, I need requests made to "www.sitename.com" and "www.sitename.com/index.php" to route to the homepage. I need requests made to "www.sitename.com/sell" to route to the sell page.

And then the hardest part, I need requests made to "www.sitename.com/buy" to route to "buy.php", but requests made to "www.sitename.com/buy/category-name" to route to "www.sitename.com/buy.php?c=12"

I know its a lot to ask, but if anyone can guide me in the right direction, it would be much appreciated.

These rules should work for you:

RewriteEngine On
RewriteBase /

# skip admin OR any valid file/dir
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR] 
RewriteCond %{REQUEST_URI} /admin/ [NC]
RewriteRule ^ - [L]

RewriteRule ^(sell|buy)/?$ $1.php [NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(buy)/([^/]+)/?$ $1.php?c=$2 [L,NC,QSA]

Try this:

RewriteEngine On  

RewriteCond %{SCRIPT_FILENAME} !-d  
RewriteCond %{SCRIPT_FILENAME} !-f  

RewriteRule ^sell$ ./sell.php
RewriteRule ^buy$ ./buy.php
RewriteRule ^buy/(.*)$ ./buy.php?c=$1

If the category is always numeric replace the (.*) with (\\d+)

I think this hope /* copy and paste below, save the name. htaccess */

RewriteEngine On
RewriteBase /yousite.com/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
#ex1:http://yousite.com/buy/3
RewriteRule ([a-zA-Z_-]+)/([0-9]?) buy.php?c=$1

/* and in your file php */

<!-- #ex1:http://yousite.com/buy/3 -->
<a href="<?php echo "buy/3" ?>">buy/3</a>

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