This is a discussion on mod rewrite issues within the Apache Web Server forums, part of the Web Server and Related Forums category; How the heck do I get mod rewrite to add extensions to any file? RewriteRule ^(.*)$ $1\.php [nc] To me ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
How the heck do I get mod rewrite to add extensions to any file?
RewriteRule ^(.*)$ $1\.php [nc] To me that should find any group of characters(such as any path) and then add the .php to the end? But this doesn't work and gives me a server error. If I remove the $ in front of the 1 then it does but ofcourse all my links end up as 1.php I can also do something like RewriteRule ^3(.*)$ $1\.php [nc] and all my links will be redirected to 3---.php What I want is to take [url] into [url].php (without query string and or other special chars such as ([^&?]&) or [QSA]) I just can't understand why the following does not work ;/ RewriteRule ^(.*)$ $1\.php [nc] $1 should be whatever is matched by (.*)? (which should be the whole url?) In any case I can't see how its an error? Thanks, Jon |
|
|||
|
On May 3, 6:36 am, "Jon Slaughter" <Jon_Slaugh...@Hotmail.com> wrote:
> How the heck do I get mod rewrite to add extensions to any file? > > RewriteRule ^(.*)$ $1\.php [nc] > > To me that should find any group of characters(such as any path) and then > add the .php to the end? But this doesn't work and gives me a server error. > If I remove the $ in front of the 1 then it does but ofcourse all my links > end up as 1.php > > I can also do something like > > RewriteRule ^3(.*)$ $1\.php [nc] > > and all my links will be redirected to 3---.php > > What I want is to take > > [url] into [url].php > > (without query string and or other special chars such as ([^&?]&) or [QSA]) > > I just can't understand why the following does not work ;/ > > RewriteRule ^(.*)$ $1\.php [nc] > > $1 should be whatever is matched by (.*)? (which should be the whole url?) > In any case I can't see how its an error? > > Thanks, > Jon I think its just a case of recursion. ..* as you say matches the request uri, so whatever it is rewritten to will again be matched, again and again... what you need is a condition along the lines of "if it doesnt have .php extension then rewrite to add it" ReWriteCond !\.php$ ReWriteRule (.*) $1\.php as an aside RewriteRule ^3(.*)$ $1\.php [nc] will actually be written to ---.php (the 3 isnt saved to $1) since this doesnt itself start with 3 that's why it works. (for all requests which dont have a repeated 3, like 33/file anyway) hope that helps. oh yes you dont need a ^$ around the .* as matching is greedy and matches everything as you say, the start char to the finish char. |
|
|||
|
"shimmyshack" <matt.farey@gmail.com> wrote in message news:1178175808.621767.146990@o5g2000hsb.googlegro ups.com... > On May 3, 6:36 am, "Jon Slaughter" <Jon_Slaugh...@Hotmail.com> wrote: >> How the heck do I get mod rewrite to add extensions to any file? >> >> RewriteRule ^(.*)$ $1\.php [nc] >> >> To me that should find any group of characters(such as any path) and then >> add the .php to the end? But this doesn't work and gives me a server >> error. >> If I remove the $ in front of the 1 then it does but ofcourse all my >> links >> end up as 1.php >> >> I can also do something like >> >> RewriteRule ^3(.*)$ $1\.php [nc] >> >> and all my links will be redirected to 3---.php >> >> What I want is to take >> >> [url] into [url].php >> >> (without query string and or other special chars such as ([^&?]&) or >> [QSA]) >> >> I just can't understand why the following does not work ;/ >> >> RewriteRule ^(.*)$ $1\.php [nc] >> >> $1 should be whatever is matched by (.*)? (which should be the whole >> url?) >> In any case I can't see how its an error? >> >> Thanks, >> Jon > > I think its just a case of recursion. > > .* as you say matches the request uri, so whatever it is rewritten to > will again be matched, again and again... > what you need is a condition along the lines of "if it doesnt > have .php extension then rewrite to add it" > ReWriteCond !\.php$ > ReWriteRule (.*) $1\.php > I still get the same server error when I try that ;/ my .htaccess file is DirectoryIndex Index.html index.html Index.htm index.htm Index.php index.php IndexIgnore */* RewriteEngine on Options +FollowSymlinks ReWriteCond !\.php$ ReWriteRule (.*) $1\.php Seems to be an issue with the rewritecond. I'll read up on it and see. I don't see how its infinite recursion though? Why is it matched after it has changed? Is that the nature of mod rewrite? I would think that it would apply the rule once to the url? (just curious) Thanks, Jon |
|
|||
|
"shimmyshack" <matt.farey@gmail.com> wrote in message news:1178175808.621767.146990@o5g2000hsb.googlegro ups.com... > On May 3, 6:36 am, "Jon Slaughter" <Jon_Slaugh...@Hotmail.com> wrote: >> How the heck do I get mod rewrite to add extensions to any file? >> >> RewriteRule ^(.*)$ $1\.php [nc] >> >> To me that should find any group of characters(such as any path) and then >> add the .php to the end? But this doesn't work and gives me a server >> error. >> If I remove the $ in front of the 1 then it does but ofcourse all my >> links >> end up as 1.php >> >> I can also do something like >> >> RewriteRule ^3(.*)$ $1\.php [nc] >> >> and all my links will be redirected to 3---.php >> >> What I want is to take >> >> [url] into [url].php >> >> (without query string and or other special chars such as ([^&?]&) or >> [QSA]) >> >> I just can't understand why the following does not work ;/ >> >> RewriteRule ^(.*)$ $1\.php [nc] >> >> $1 should be whatever is matched by (.*)? (which should be the whole >> url?) >> In any case I can't see how its an error? >> >> Thanks, >> Jon > > I think its just a case of recursion. > > .* as you say matches the request uri, so whatever it is rewritten to > will again be matched, again and again... > what you need is a condition along the lines of "if it doesnt > have .php extension then rewrite to add it" > ReWriteCond !\.php$ > ReWriteRule (.*) $1\.php > > as an aside > RewriteRule ^3(.*)$ $1\.php [nc] > will actually be written to > ---.php > (the 3 isnt saved to $1) > since this doesnt itself start with 3 > that's why it works. (for all requests which dont have a repeated 3, > like 33/file anyway) > > hope that helps. > oh yes you dont need a ^$ around the .* as matching is greedy and > matches everything as you say, the start char to the finish char. > strange, # Find single files like mypage RewriteCond $1 !images|downloads RewriteRule ^(.*)$ /$1.php [L] fails but # Find single files like mypage RewriteCond $1 !images|downloads RewriteRule ^([^\.]+)$ /$1.php [L] works, why is (.*) screwing up? I guess its obviously not doing what I think it should. |
|
|||
|
On May 3, 9:29 am, "Jon Slaughter" <Jon_Slaugh...@Hotmail.com> wrote:
> "shimmyshack" <matt.fa...@gmail.com> wrote in message > > news:1178175808.621767.146990@o5g2000hsb.googlegro ups.com... > > > > > On May 3, 6:36 am, "Jon Slaughter" <Jon_Slaugh...@Hotmail.com> wrote: > >> How the heck do I get mod rewrite to add extensions to any file? > > >> RewriteRule ^(.*)$ $1\.php [nc] > > >> To me that should find any group of characters(such as any path) and then > >> add the .php to the end? But this doesn't work and gives me a server > >> error. > >> If I remove the $ in front of the 1 then it does but ofcourse all my > >> links > >> end up as 1.php > > >> I can also do something like > > >> RewriteRule ^3(.*)$ $1\.php [nc] > > >> and all my links will be redirected to 3---.php > > >> What I want is to take > > >> [url] into [url].php > > >> (without query string and or other special chars such as ([^&?]&) or > >> [QSA]) > > >> I just can't understand why the following does not work ;/ > > >> RewriteRule ^(.*)$ $1\.php [nc] > > >> $1 should be whatever is matched by (.*)? (which should be the whole > >> url?) > >> In any case I can't see how its an error? > > >> Thanks, > >> Jon > > > I think its just a case of recursion. > > > .* as you say matches the request uri, so whatever it is rewritten to > > will again be matched, again and again... > > what you need is a condition along the lines of "if it doesnt > > have .php extension then rewrite to add it" > > ReWriteCond !\.php$ > > ReWriteRule (.*) $1\.php > > I still get the same server error when I try that ;/ > > my .htaccess file is > > DirectoryIndex Index.html index.html Index.htm index.htm Index.php index.php > IndexIgnore */* > > RewriteEngine on > Options +FollowSymlinks > > ReWriteCond !\.php$ > ReWriteRule (.*) $1\.php > > Seems to be an issue with the rewritecond. I'll read up on it and see. > > I don't see how its infinite recursion though? Why is it matched after it > has changed? Is that the nature of mod rewrite? I would think that it would > apply the rule once to the url? (just curious) > > Thanks, > Jon I forgot to tell rewritecond what to test _on_!! ReWriteCond %{REQUEST_URI} !\.php$ ReWriteRule (.*) $1\.php you might find it fun to test on %{REQUEST_FILENAME} too depending on your needs my apologies. |
|
|||
|
On May 3, 9:29 am, "Jon Slaughter" <Jon_Slaugh...@Hotmail.com> wrote:
> "shimmyshack" <matt.fa...@gmail.com> wrote in message > > news:1178175808.621767.146990@o5g2000hsb.googlegro ups.com... > > > > > On May 3, 6:36 am, "Jon Slaughter" <Jon_Slaugh...@Hotmail.com> wrote: > >> How the heck do I get mod rewrite to add extensions to any file? > > >> RewriteRule ^(.*)$ $1\.php [nc] > > >> To me that should find any group of characters(such as any path) and then > >> add the .php to the end? But this doesn't work and gives me a server > >> error. > >> If I remove the $ in front of the 1 then it does but ofcourse all my > >> links > >> end up as 1.php > > >> I can also do something like > > >> RewriteRule ^3(.*)$ $1\.php [nc] > > >> and all my links will be redirected to 3---.php > > >> What I want is to take > > >> [url] into [url].php > > >> (without query string and or other special chars such as ([^&?]&) or > >> [QSA]) > > >> I just can't understand why the following does not work ;/ > > >> RewriteRule ^(.*)$ $1\.php [nc] > > >> $1 should be whatever is matched by (.*)? (which should be the whole > >> url?) > >> In any case I can't see how its an error? > > >> Thanks, > >> Jon > > > I think its just a case of recursion. > > > .* as you say matches the request uri, so whatever it is rewritten to > > will again be matched, again and again... > > what you need is a condition along the lines of "if it doesnt > > have .php extension then rewrite to add it" > > ReWriteCond !\.php$ > > ReWriteRule (.*) $1\.php > > I still get the same server error when I try that ;/ > > my .htaccess file is > > DirectoryIndex Index.html index.html Index.htm index.htm Index.php index.php > IndexIgnore */* > > RewriteEngine on > Options +FollowSymlinks > > ReWriteCond !\.php$ > ReWriteRule (.*) $1\.php > > Seems to be an issue with the rewritecond. I'll read up on it and see. > > I don't see how its infinite recursion though? Why is it matched after it > has changed? Is that the nature of mod rewrite? I would think that it would > apply the rule once to the url? (just curious) > > Thanks, > Jon and to answer your question, yes the nature of rewrites is that only when the request no longer matches the output does it stop, unless you use modifying flags like NC no chain NS no internal subrequests L last PT pass through, as well as rewritecond of course. you may allso "save" matches using bracket notation in the rewritecond and if you do, you then might not need to match again in the rewrite line, any matches on the rewritecond line are held in %1, %2, %3 ... you can mix the two as well, so you might have a rewrite line that rewrites urls to %1$1/%2\.html for instance. mod_rewrite is the equivalent of a emotionally complex partner: provided you understand them, the reward is worth it. |
|
|||
|
>
> I forgot to tell rewritecond what to test _on_!! > > ReWriteCond %{REQUEST_URI} !\.php$ > ReWriteRule (.*) $1\.php > What I'm using is this, which is essentially what you have but now works like I want it. Now I just have to limit it to only the root or only its sub dir. I guess I can do this by creating another rewrite condition. #RewriteCond %{REQUEST_URI} !\.php$ RewriteRule ^(.*)[/]$ /Index.php\?$1/Main.php > you might find it fun to test on %{REQUEST_FILENAME} too depending on > your needs > I will try to look into it more when I can. Its a little confusing because the errors are not specific so I have no idea whats going on sometimes. > my apologies. > Don't worry... Not like your getting paid to help ;) Thanks, Jon |
|
|||
|
On May 3, 11:08 am, "Jon Slaughter" <Jon_Slaugh...@Hotmail.com> wrote:
> > I forgot to tell rewritecond what to test _on_!! > > > ReWriteCond %{REQUEST_URI} !\.php$ > > ReWriteRule (.*) $1\.php > > What I'm using is this, which is essentially what you have but now works > like I want it. Now I just have to limit it to only the root or only its sub > dir. I guess I can do this by creating another rewrite condition. > #RewriteCond %{REQUEST_URI} !\.php$ > > RewriteRule ^(.*)[/]$ /Index.php\?$1/Main.php > > > you might find it fun to test on %{REQUEST_FILENAME} too depending on > > your needs > > I will try to look into it more when I can. Its a little confusing because > the errors are not specific so I have no idea whats going on sometimes. > > > my apologies. > > Don't worry... Not like your getting paid to help ;) > > Thanks, > Jon have you use the rewritelog set on level 9 to see everything that might be going on, using rewrites in .htaccess files is annoying, I dont use htaccess for this reason, often you wil need something like ReWriteBase / to modify the scope and this is all too much for me, I use rewrites within directory blocks in the main httpd.conf and have used allowoverride none to ban htaccess serverwide. you could try this for instance if all you are doing is sending each URI to the index.php file: RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L,NS] this works for all request filenames that are not files or folders which exist at the place they are requested. you would then obtain the value of $_SERVER['REQUEST_URI'] within your inde.php script and spilt it by / or use parse_url() to obtain the URI initially requested and then act on this in the way your app needs. |
|
|||
|
"shimmyshack" <matt.farey@gmail.com> wrote in message news:1178190192.685606.28820@q75g2000hsh.googlegro ups.com... > On May 3, 11:08 am, "Jon Slaughter" <Jon_Slaugh...@Hotmail.com> wrote: >> > I forgot to tell rewritecond what to test _on_!! >> >> > ReWriteCond %{REQUEST_URI} !\.php$ >> > ReWriteRule (.*) $1\.php >> >> What I'm using is this, which is essentially what you have but now works >> like I want it. Now I just have to limit it to only the root or only its >> sub >> dir. I guess I can do this by creating another rewrite condition. >> #RewriteCond %{REQUEST_URI} !\.php$ >> >> RewriteRule ^(.*)[/]$ /Index.php\?$1/Main.php >> >> > you might find it fun to test on %{REQUEST_FILENAME} too depending on >> > your needs >> >> I will try to look into it more when I can. Its a little confusing >> because >> the errors are not specific so I have no idea whats going on sometimes. >> >> > my apologies. >> >> Don't worry... Not like your getting paid to help ;) >> >> Thanks, >> Jon > > have you use the rewritelog set on level 9 to see everything that > might be going on, using rewrites in .htaccess files is annoying, I > dont use htaccess for this reason, often you wil need something like > ReWriteBase / to modify the scope and this is all too much for me, I > use rewrites within directory blocks in the main httpd.conf and have > used allowoverride none to ban htaccess serverwide. > > you could try this for instance if all you are doing is sending each > URI to the index.php file: > RewriteEngine On > RewriteBase / > RewriteCond %{REQUEST_FILENAME} !-f > RewriteCond %{REQUEST_FILENAME} !-d > RewriteRule . /index.php [L,NS] > > this works for all request filenames that are not files or folders > which exist at the place they are requested. > you would then obtain the value of $_SERVER['REQUEST_URI'] within your > inde.php script and spilt it by / or use parse_url() to obtain the URI > initially requested and then act on this in the way your app needs. Unfortunately the code is ment for a web host service so I can't touch httpd.conf... The second problem is that my directory structure was created before I knew to much about php and so most of the stuff can't be changed, atleast easily. I'm still messing with it but I think I'm on the right track now. Thanks, Jon |
|
|||
|
"shimmyshack" <matt.farey@gmail.com> wrote in message news:1178190192.685606.28820@q75g2000hsh.googlegro ups.com... > On May 3, 11:08 am, "Jon Slaughter" <Jon_Slaugh...@Hotmail.com> wrote: >> > I forgot to tell rewritecond what to test _on_!! >> >> > ReWriteCond %{REQUEST_URI} !\.php$ >> > ReWriteRule (.*) $1\.php >> >> What I'm using is this, which is essentially what you have but now works >> like I want it. Now I just have to limit it to only the root or only its >> sub >> dir. I guess I can do this by creating another rewrite condition. >> #RewriteCond %{REQUEST_URI} !\.php$ >> >> RewriteRule ^(.*)[/]$ /Index.php\?$1/Main.php >> >> > you might find it fun to test on %{REQUEST_FILENAME} too depending on >> > your needs >> >> I will try to look into it more when I can. Its a little confusing >> because >> the errors are not specific so I have no idea whats going on sometimes. >> >> > my apologies. >> >> Don't worry... Not like your getting paid to help ;) >> >> Thanks, >> Jon > > have you use the rewritelog set on level 9 to see everything that > might be going on, using rewrites in .htaccess files is annoying, I > dont use htaccess for this reason, often you wil need something like > ReWriteBase / to modify the scope and this is all too much for me, I > use rewrites within directory blocks in the main httpd.conf and have > used allowoverride none to ban htaccess serverwide. > > you could try this for instance if all you are doing is sending each > URI to the index.php file: > RewriteEngine On > RewriteBase / > RewriteCond %{REQUEST_FILENAME} !-f > RewriteCond %{REQUEST_FILENAME} !-d > RewriteRule . /index.php [L,NS] > > this works for all request filenames that are not files or folders > which exist at the place they are requested. > you would then obtain the value of $_SERVER['REQUEST_URI'] within your > inde.php script and spilt it by / or use parse_url() to obtain the URI > initially requested and then act on this in the way your app needs. > Maybe you can help me with one last thing? I'm having trouble with query strings, When I try to access something like /Home?Page=3 it rewrites it to /Home/?Page=3 I should be able to rewrite it to remove the /? so that its just ? but can't seem to do it as it just breaks everything ;/ I'm not sure why its rewriting it automatically in the first place? Thanks, Jon |