Rewrite rule looping indefinitely - php

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?

Related

Wordpress rewrite rule involving index.php doesn't work

Before I start, I'd like to say that I did search for topics similar to my problem and tried all solutions provided, but my problem still persists, so I had to post my own question.
Anyway, inside our wordpress site, we have a page called site.com/shops and we want to put a parameter to it. After that we want to use a rewrite rule so that site.com/tx will load site.com/?pagename=shops&state=tx. We put this on our .htaccess:
RewriteRule ^tx(/)?$ index.php?pagename=shops&state=tx[L]
For some reason this doesn't work like we intended. I checked to see if index.php was the problem by changing it to
RewriteRule ^tx(/)?$ test.php?pagename=shops&state=tx[L]
That worked, so I believe the problem is that it doesn't work if index.php was used. How do I make it work using index.php? TIA.
Replace .htaccess code with below code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress_test/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress_test/index.php [L]
</IfModule>
i hope it's works

.htaccess url rewriting not working?

I'll be honest in saying I have very little experience with .htaccess as I've always wanted to stay away from it as best I can. However, I've recently wanted to tidy up my urls and I've found that it's possible through .htaccess and rewriting.
Basically, I want to rewrite a url like:
www.mysite.com/profile.php?id=48194
To something like:
www.mysite.com/profile/48194
Here's the code I have currently:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^profile/(.*)/$ profile.php?id=$1
The line I'm trying to use is on the very bottom, RewriteRule ^profile/(.*)/$ profile.php?id=$1. The rest is used to remove the page extensions from the urls. I've changed $1 to $2 thinking perhaps it was conflicting with the code above, but nothing changed.
I also removed all the code except for RewriteEngine on and the last line thinking maybe the codes were conflicting but, again, nothing changed or worked. The rest of the code does work, removing the extensions from urls that is, so I know the rewrite thing is on.
Could someone try to break down and explain what I did wrong and how all this works? As well as providing a working example of the thing I'm trying to accomplish?
Thanks in advance!
Change order of your rules and use MultiViews option. Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.
RewriteEngine on
RewriteBase /
RewriteRule ^profile/([^/]+)/?$ profile.php?id=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

URL rewrite shows query string to client

I'm implementing friendly-URLs using mod_rewrite in Apache HTTPD. My .htaccess file looks like this:
RewriteEngine On
rewritecond %{REQUEST_URI} !((.php|.htm|.css|.jpg|.png|.gif|.js|.ico)|/static/|/uploads/)
RewriteRule (.*) /main.php?path=$1 [QSA,L]
(I know it's a bit of a mess, it's related to legacy code that I'll be fixing later)
This works perfectly if the user puts in a URL that looks like
http://example.com/test/
However, if the user forgets the trailing slash
http://example.com/test
then they get redirected to
http://example.com/test/?path=test
This makes the URLs quite ugly, and I'd like to fix it, however I don't have enough of an understanding of mod_rewrite to know what's wrong. Any chance anyone could help me?
Thanks!
It is happening due to mod_dir adding a trailing slash in front of directories. You should use RewriteCond to skip rewriting existing files/directories like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /main.php?path=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+?)/$ /main.php?path=$1 [QSA,L]
In the end I rearranged my directory structure and tidied up my .htaccess file to simply contain
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /main.php?path=$1 [QSA]
This does mean I'll have to do some more manual 404 error handling, but that's not really a big issue for me as the nature of my URL structure meant I'd have had to do that anyway.
Thanks heaps to anubhava for helping me work through this issue, I've marked his answer as correct even though it was really his comments that helped me.
EDIT: It's very important to use REQUEST_FILENAME instead of REQUEST_URI for the RewriteConds. I'm not sure why, but it doesn't work if you use the wrong ones.

.htaccess mod_rewrite for pretty urls not working

Trying to get www.example.com/test.php?archives=testing to www.example.com/archives/testing
According to godaddy they have mod_rewrite enabled by default
Here is my .htaccess file:
rewriteengine on
rewritecond %{HTTP_HOST} ^example.com$
rewriterule ^example\/(.*)$ "http\:\/\/www\.example\.com\/$1" [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^archives/(\w+)$ test.php?archives=$1 [L]
However this is not working, when i go to www.example.com/archives/test I get a 404, suggestions
I just left this in a comment but i might as well put it here so its seen easier. I wrote an answer for someone thats helped others out over time, and in the end this isn't exactly an answer to what your asking however its more of a stepping stone in the direction. The original question was asked how to work with short url strings and make them work in a fashion like your looking for, but rather copy and paste that answer here. Ill let you go there and read over it.
Its not to go without saying you will need to alter the rule a little for your specific needs but it will in the end serve its purpose for getting you where you want to be.
PHP dynamic DB page rewrite URL
You need some rewrite conditions to specify when this rule will be used. Without them, you will keep running the same rewrite rule indefinitely, giving you an error. Try:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^archives/(\w+)$ test.php?archives=$1 [L]
How about this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^archives/(.+)$ /test.php?archives=$1 [L]
</IfModule>
Without is being super clear what you are trying to get from your rewrites I suggest:
Options -MultiViews
ErrorDocument 404 default
RewriteBase /

Question mark in URL for PHP variables makes the link broken. Any idea why?

I don't know what changed in the past--this used to work:
Accessing a URL on my server like the following, doesn't work: http://www.domain.com/folder/file.php?variable=a&variable2=b
I'm getting a "Not found The requested address 406.shtml was not found on this server." message.
However, if I access this, it works:
http://www.domain.com/folder/file.php
Adding the question mark after file.php is what makes it break. I have never experienced a problem like this before. At first I thought that .htaccess had something to do with it, but I know as a fact that it hasn't been edited at all in the past.
Any ideas? I'm using CakePHP, but I doubt that has anything to do with it; this has worked before. All suggestions are welcome!
EDIT:
The /app/webroot .htaccess file has this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
As far as I know, this hasn't changed at all, and the URL worked with this. The .htaccess file right under /public_html/ contains this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
You've got an error in your apache config. It's attempting to give you the 406 error page, but can't find it. If you fix that you may get a more informative error.
This link may help you with the root of the problem: http://urbangiraffe.com/2005/08/20/mysterious-406-error/
It could also be caused by mod_security. If it's not your server you should ask your hosting provider.
99.9% probability that this is being caused by a change in your htaccess file. Something is checking for a query string and redirecting to or trying to load a file that doesn't exist.

Categories