开发者

Mod_rewrite giving users own url, need to exclude some cases

开发者 https://www.devze.com 2023-02-08 12:36 出处:网络
I\'m building a small local social good community website where every project has his own url, such that when username=\'foo\' the URL becomes http://website.com/foo similar to Twitter. Even though I\

I'm building a small local social good community website where every project has his own url, such that when username='foo' the URL becomes http://website.com/foo similar to Twitter. Even though I'm not experienced with htaccess I've managed to make it work to a large extent. However I am now running into problems when someone types in http://website.com/subdomain/foo. This now also tries to find the username and display the page, but I don't want that to work and want to show them our 404 page.

Also, on some subdomain's (i.e. about) we have other pages such as 'ourstory.php'. I'd really like to display 'about/ourstory' instead of about/ourstory.php, but only for selected subdomains.

Finally, when having the structure 'website.com/subdomain/subdomain', it generates internal server errors when requiring other php files, even when absolute paths are used.

Here is my current mod_rewrite, I hope you can understand what I try to do and feel free to optimize, I've probably made some beginner mistakes.

This mod rewrite does three things:

1. Remove 'www' infront of address;

2. The second makes sure that when they just type the homep开发者_Python百科age, it takes them to the homepage(removing this, would result in an empty homepage for some reason, can this be done in another way?);

3. When a name is typed that isn't a subdomain, it redirects them to the profile page and makes the url the username.

<IfModule mod_rewrite.c>
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
  RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</IfModule>
<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteCond %{REQUEST_FILENAME} !-d 
 RewriteCond %{REQUEST_URI} !-f
 RewriteCond %{REQUEST_URI} !-d 
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteRule ^(.+)/$ http://website.com/$1 [R=301,L]
</IfModule>

<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteCond %{REQUEST_FILENAME} !-d 
 RewriteCond %{REQUEST_URI} !-f
 RewriteCond %{REQUEST_URI} !-d 
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_URI} !\.(css|gif|ico|jpg|js|png|swf|txt)$
 RewriteRule ^(.{5,16})$ profile.php?scrname=$1
 RewriteRule ^(.+)(\.php|\.html)/$  /$1$2 [R=301,L]
</IfModule>

Again, any help will be greatly appreciated.


You might find it easier to maintain if you use a front controller instead. Basically, you would redirect all requests that aren't for static files to a single php script, which can then perform advanced rules more easily expressed in code than mod_rewrite rules.

Your .htaccess might then look a little like:

RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d 
RewriteRule ^(.*)$ index.php?q=$1

In your PHP code you could then process this by examining $_GET['q'] to decide which page to render.


I have made some progress since I posed the question. Here is the (seemingly very simple) mod_rewrite I ended up with:

<IfModule mod_rewrite.c>
   //mod_rewrite by Marc
   RewriteEngine On
   RewriteCond %{HTTPS} !=on
   RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
   RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
   RewriteCond %{SCRIPT_FILENAME} !-d  
   RewriteCond %{SCRIPT_FILENAME} !-f 
   RewriteRule ^(\w+)$ ./project.php?id=$1
</IfModule>

It does everything I wanted, fixed the internal server errors and therefore I don't need the rewrite to remove the .php anymore. I hope it can help someone else.

0

精彩评论

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