Forum Moderators: phranque
I wrote the following .htaccess file, it works, but I'm not sure if it's 100% correct or if I messed with it until it worked ;)
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI}!index\.php$
RewriteCond %{REQUEST_URI}!uns\.css$
RewriteCond %{REQUEST_URI}![a-zA-Z0-9]\.gif¦jpg$
RewriteRule ^(.*)$ index.php?page=$1 [L]
What it's supposed to do is: All files are redirected to index.php?page=filename. My php script checks the $page variable and then includes different files.
But when I do this my css file is not loaded, neither my images. So I included the other two conditions. Is this correct the way I wrote them or can you do it better?
What I basically want is that evreything you type into the addressbar should be redirected to my index.php. All other files like stylesheets and pictures should load without problems. When a file not exists, I want it also to be redirected to index.php.
Thanks in advance!
> When a file not exists, I want it also to be redirected to index.php
Be very careful if you do this. Index.php will have to contain logic to correctly report non-existent pages along with the correct 404-Not Found or 410-Gone server header.
If you return a page no matter what URL is requested from your site, then search engines will consider your site to be a "spider trap" -- a site with an infinite URL-space. They will avoid spidering it deeply so as not to get stuck in it forever.
As for your code, you can shorten it by using the local OR across all the excluded files and filetypes:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !(^/index\.php¦^/uns\.css¦\.gif¦\.jpg)$
RewriteRule (.*) /index.php?page=$1 [L]
Jim
thanks so far, but with your script I only get a 404 Error. Apache tells me it cannot find index.php.
Is there a better way to do this? My goal is to have search engine/user friendly URLs. So that the URL www.domain.com/example.html is redirected to www.domain.com/index.php?page=example.html. The index.php ist supposed to check the $page variable and then include the right files.
If a requested page is not defined in the php script, it just returns the mainpage, so no 404 error reports.
With my htaccess script apache redirects all requests, even for subdirectories of my directory, so that images and stylesheets are not loaded.
So my question: Is there a better way to do this?
Thanks in advance!
Well, that would be a rather large problem... Where is your index.php located in relation to the directory where your mod_rewrite code is placed? The code I posted should work fine if they are both in the Web root ("home page" directory) of your site, and it should do exactly what you describe.
> If a requested page is not defined in the php script, it just returns the mainpage, so no 404 error reports.
To be clear, I am warning you that if you do this, your search engine rankings may suffer badly.
Jim
Well, that would be a rather large problem... Where is your index.php located in relation to the directory where your mod_rewrite code is placed? The code I posted should work fine if they are both in the Web root ("home page" directory) of your site, and it should do exactly what you describe.
The index.php ist in the same directory as my htaccess mod_rewrite code. In relation to my root dir both files are in /temp.
Another problem ist, that the rewrite script reacts on every request, even request for subdirectories. How can I restrain it to the dir where it resides?
To be clear, I am warning you that if you do this, your search engine rankings may suffer badly.
How would a search engine notice that? The redirection is inside the Apache server, if I'm now mistaken. Since index.php has a different content each time, I don't think the SE will realize it. Or am I making some mistake here?
Do you have a better idea to make my URLs user/SE friendly?
Thanks!
If your code and index.php are in /temp, then the code has to take that into account:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !(^/[b]temp/[/b]index\.php¦^/[b]temp/[/b]uns\.css¦\.gif¦\.jpg)$
RewriteRule (.*) /[b]temp/[/b]index.php?page=$1 [L]
> How would a search engine notice that?
By testing the server response for "nonsense" URLs -- Which several search engines do, occasionally.
I just wanted to warn you about this technique. It is a bad idea. But do as you please.
> Do you have a better idea to make my URLs user/SE friendly?
No, this is a good technique -- You just have to get it set up correctly for your site.
Jim
What I do now is this:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} \.html$
RewriteRule ^(.*)$ index.php?page=$1 [L]
Clean and simple, only .html files are redirected to my php script, so that every request to a nonexisting file or dir will just be answered with a 404. My php script will answer all requests to html files, that are not existing, also with a 404. So no search engine should find any suspect. I just need those .html filenames to determine the output page that the user requested.
Is this better? At least it works smoothly, and I don't see what problems could come.
And the best about this is that I learned quite a thing about regular expressions and mod_rewrite in general :)
Thanks!
Better? Yes, because it does what you want it to do... :)
> And the best about this is that I learned quite a thing about regular expressions and mod_rewrite in general.
...and so if you decide you need to change it, you can! (This is one reason our charter says we won't write your code for you - only help you to get it to work properly.)
Glad you got it working!
Jim