I'm stuck in what appears to be an infinite redirecting loop, caused by rewrite conditions in my htaccess file.
The goal was first to get rid of the .php extension, which worked.
Then I wanted to rewrite site.com/profile?user=username to site.com/username, which also worked.
But now, only site.com/username works. All other urls are stuck in a loop 'too many redirects occurred trying to open...' except if I write the .php extension again. Then the page opens.
How can I cumulate both rewrite rules and have them both work together?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ /profile.php?user=$1 [L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Any help much appreciated, as I really do not understand this...
Thanks in advance!
Related
I have a problem with rewriting urls to my files. What I am trying to do is making my little shop system a bit more SEO friendly. My problem is that it sometimes works and sometimes it doesn't. I have no idea what I should do or what I am doing wrong.
My UPDATED .htaccess file:
ErrorDocument 404 /shop/404.html
RewriteEngine On
RewriteBase /shop/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^produkte/?(.*)$ products.php$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^produkt/?(.*)$ product.php?url=$1 [L]
RewriteRule ^(.*)/(css|js|img|fonts)/(.*)?$ /shop/$2/$3 [L,QSA,R=301]
For example the /shop/products link is not working but /shop/products/ is.
And for some reason if I want to open the link /shop/products?cat=besteck its redirecting me to: localhost/D:/xampp/htdocs/shop/products.php?cat=besteck but If I capitalize the b it's working fine..
I have no Idea what to do, please help me! (And dont just give a working code snippet explain why mine fails and yours works)
EDIT:
Just to clear things up I want /products, /products/ and /products?some_get_query to redirect to my products.php file. /product/some_seo_url should be redirected to product.php?url=some_seo_url. I tried adding a question mark after the forward slash in my RewriteRule and I also tried putting the ^products rule above the ^product rule. Nothing worked yet.
EDIT 2:
I updated my .htaccess code above and now nearly everything works. The only thing that still doesn't work is when I open /shop/products/?cat=fish or /shop/product/some_product, my resources aren't loading!
ErrorDocument 404 /shop/404.html
RewriteEngine On
RewriteBase /shop/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^products/?(.*)$ products.php?$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^product/?(.*)$ product.php?url=$1 [L]
RewriteRule ^(.*)/(css|js|img|fonts)/(.*)?$ /shop/$2/$3 [L,QSA,R=301]
First off your product rule also matches products so a rewrite like:
products/cat/fish becomes product.php?url=s/cat/fish
Which is not what you want, the easiest way to avoid that is to reverse the order so that the products rewrite comes before the product one but I've also added the Last flag ([L]) to be on the safe side; besides, once it's got the match you want it's better for it to stop looking.
To prevent recursive rewrite loops you need to specify that the rewrite only occurs when the redirect is not an existing file or directory (otherwise your product rewrite matches product.php and it loops - forever). That's what those RewriteCond lines signify.
Other than that it seems OK.
I just want a simple redirect to clean up the url's on a site.
e.g.
I want ajhtestserver.com/registration/ to redirect to ajhtestserver.com/registration.php
It should be easy and I have successfully used .htaccess rewrites on other sites but for some reason it just will not work for me today.
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^registration[/]$ registration.php [NC,L] # Handle requests for "registration"
I am sure it is something simple that I am missing but I basically just copied what I have on other sites that work fine for me so I am confused as to why it just refuses to work for me here (gives me The requested URL /ajhtestserver/registration/ was not found on this server. error). Just one of those days :(
Any help is appreciated.
Thanks,
Adam
if you use apache ,first you should enable rewrite_mode in http.conf or ...\
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^registration/(.*)$ registration.php/$1 [L]
check .htaccess syntax or rewrite mode.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)[/]$ $1.php [L]
Well it didn't seem to like it when the redirect source word and target filename were the same word but this works...
RewriteRule ^([a-zA-Z\ ]+)[/]?$ $1.php [NC,L]
And that is actually a better solution anyway as it doesn't require a separate rule for each page.
Though I never did figure out why it didn't like it the original way.
I'm trying to removing .php extension from the url using .htaccess i've found a view sites that told me how to do this and i'm using;
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
However when i go to mydomain.com/user/settings (that would be mydomain.com/user/settings.php without the .htaccess)
It works fine. However when i go to mydomain.com/user/settings/ (trending splash on the end) it gives my custom 404 error page i've looked around on here (& Similar Questions) ask and google but can't really seem to work out what part i'm missing out?
Thank You,
Flexer
Try this to handle the trailing slash
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+?)/?$ $1.php [NC,L]
Perhaps someone can help me with this before I go completely crazy with it. I have a site with two urls which I want to redirect.
I want all traffic except the gallery pages to go to index.php.
This is my htaccess:
php_flag magic_quotes_gpc Off
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^(.*)$ /index.php?/$1
RewriteRule ^gallery(/((([a-zA-Z0-9-]+)(/(\d+))?)/?)?)?$ gallery.php?groupId=$4&showpage=$6 [NC,QSA,L]
This works except for one part. The rewrite rule for the gallery is not fully working. It is sending through the groupId to the gallery.php script but I am not getting through the showpage argument. In fact when the showpage is included in the url I get a 404. So for instance.
These work and are handled correctly by the rewrite rule
gallery
gallery/
gallery/mygroup
gallery/mygroup/
This does not work and throws 404's.
gallery/mygroup/2
Nothing I do seems to fix this and I would appreciate your help on this. If I set the showpage. The gallery script works if I feed it the old
gallery.php?groupId=mygroup&showpage=2
so I am sure the htaccess rule is not catching the url.
Thanks in advance.
Your regex seems a little over-the-top. Use two simpler rewrite rules instead:
RewriteRule ^gallery/([a-zA-Z0-9-]+)/?$ gallery.php?groupId=$1 [NC,QSA,L]
RewriteRule ^gallery/([a-zA-Z0-9-]+)/(\d+)/?$ gallery.php?groupId=$1&showpage=$2 [NC,QSA,L]
I have strange problem with .htaccess I can't solve:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1
RewriteRule watermelonurltest wmelon/core/works.html
</IfModule>
When watermelonurltest is accessed, it loops to something like:
http://localhost/w/watermelonurltest/index.php/2/index.php/2/index.php/2/index.php/2/index.php/2/index.php/2/index.php/2/(...)
Which suggests that RewriteRule ^(.*)$ index.php?/$1 is applied instead of RewriteRule watermelonurltest wmelon/core/works.html
I tried to swap these two rules, but then other problems like this one occur.
The problem didn't exist when the first rule was
RewriteRule ^(.*)$ index.php/$1
(no question mark after .php)
For some reason, this question mark breaks it.
I can't figure out why this happens and how to fix it - I tried googling a little, but I didn't find anything helpful.
Thanks in advance.
https://www.communitymx.com/content/article.cfm?cid=51C62&print=true
I attempted to reproduce this in a fresh setup, but using the .htaccess and rules provided, I didn't encounter a loop; the rewrite was to index.php?/watermelonurltest; confirmed by looking at the rewrite log.
What redirection does index.php do, if any?