Forum Moderators: phranque
I use mod_rewrite to create static URL on a website with the following rule
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^/([_A-Za-z0-9-]+)/?$ /index.php?id=$1 [L]
The single pages are accessible over [domain.com...]
The problem I have now is, if I enter [domain.com...] I get an Error 404 code. I could do a permanent redirection from .php to non .php , but I would have to enter a line for every single page that exists on the website. What rule do I need that would all page.php requests rewrite or redirect to page ( to a non .php ending )
Another problem is, if I enter [domain.com...] I see the site loading, but without any style sheet and the links turn into [domain.com...] , where if I click on those links I get also an Error 404 . . .
How to make all [domain.com...] requests rewite or redirect to the [domain.com...] , how to prevent page loads with trailing slashes. Or how to make that both [domain.com...] and [domain.com...] will work the same ( load the stylesheet and keep the links accurate )
Thanks in advance
Your rewriterule's regular-expressions pattern will not accept "." because it is not included in the group. Therefore, the rule wlll not rewrite any request that include a filetype, e.g. ".css".
> if I enter http://www.example.com/page/ I see the site loading, but without any style sheet and the links turn into http://www.example.com/page/page I see the site loading, but without any style sheet and the links turn into http://www.example.com/page/page
This is most likely because you are using page-relative links on those pages.
Remember that it is *the browser* that resolves relative links to full canonical form before requesting resources from those links. Because the browser believes that the page is located in the *directory* http://www.example.com/page/, it will simply append your on-page relative links to that 'root' to form the URL.
The two most common solutions are to use server-relative links, i.e. <img src="/iimages/logo.gif"> or canonical links, i.e. <img src="http://example.com/images/logo.gif">, or to *also* rewrite those image, CSS, and external JS requests to the proper and 'real' directory.
Jim
- I solved the problem with the links after someone enters the site with the trailing slash . . .
- The "style not loading" problem is also solved . . . I've entered the absolute path to the css file and to the imageson the site . . . If someone has any idea how to do it else ( with an example if possible ) I would be happy to know it.
- what is left, is only the .php thing . . . anybody got any rule for that?
Thanks
You could add another rule that accepts certain filetypes:
RewriteRule ^/([_A-Za-z0-9-]+)\.(htm¦html¦php)/?$ /index.php?id=$1 [L]
You can, of course squeeze this into one rule, but as long as you haven't reached Nirwana you might go you easy-to-read solution.
This one should (I didn't test it) match any extension from 1 to 4 characters:
RewriteRule ^/([_A-Za-z0-9-]+)\.(.{1,4})/?$ /index.php?id=$1 [L]
But the main problem is that the goal has not been clearly defined. You will likely have better luck by doing the following things:
1) Review the code posted so far, using the references provided in our forum charter [webmasterworld.com], until you fully understand what it does.
2) Make a list of URLs, or types or classes of URLs, that you wish to rewrite, and make another list of those that you don't want to rewrite. While doing this, look for patterns to emerge, such as "all the URLs I want to rewrite end in .html, .htm, '/' or have no file extension at all," or "I don't want to rewrite any URLs that are in subdirectories or end with ".php" or ".css" or ".txt".
These patterns are what we attempt to define using regular expressions in the RewriteRule pattern. The reason you want to make a "do rewrite" and a "don't rewrite" list is twofold: First, often one list will be significantly smaller than the other, and therefore easier and more efficient to code. And second, since mod_rewrite supports either postive logic (do rewrite these) or negative logic (don't rewrite these), this makes it easy to code for either case.
Without knowing exactly what classes or types of URLs you do or don't wish to rewrite, all we can do is guess here, which is not a very good use of our time or yours. This process can be improved only if you define the problem well, and take steps to understand the solutions (and by definition, these will almost always be incomplete solutions) that we might propose here.
Based on my guess described above, you can stop the new rule from looping by excluding the index.php URL from being rewritten:
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule ^/([_A-Z0-9\-]+)\..{3,5}/?$ /index.php?id=$1 [NC,L]