Redirection loop URL rewriting - mod-rewrite

I'm redirecting all my .php extensions to .html, but I'm getting a loop problem.
For example, from:
-mywebsite.com/country/france-php/paris.php
to
-mywebsite.com/country/france-php/paris.html
Here is my .htaccess:
RewriteEngine On
RewriteBase /
RewriteRule (.+)\.php$ $1.html [R=301,L]
RewriteRule (.+)\.html$ $1.php [L]
FallbackResource /index.php
When analyzing with redirection detective I get many redirections to:
-mywebsite.com/country/france-php/paris.html

Do the first redirection based on raw request received:
RewriteCond %{THE_REQUEST} ^GET\ /(.+)\.php
RewriteRule ^ /%1.html [R=301,L]
RewriteRule ^(.+)\.html$ /$1.php [L]
To load index.php as index.html too, you can add the following rewrite at the top:
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} ^(.*)/$
RewriteRule ^ %1/index.php [R=301,L]

You can try your rules this way.
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ /(.+)\.php
RewriteRule ^ %1.html? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+)\.html$ $1.php [L]

Try:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \ /+([^\?\ ]+)\.php([^\?\ ]*)
RewriteRule ^ %1.html%2 [L,R=301]
RewriteCond %{DOUMENT_ROOT}/$1.php -f
RewriteRUle ^(.+)\.html(?:(/.*)|)$ $1.php$2 [L]
The problem is that the rewrite engine loops and your two rules keep overwriting each other. By adding some conditions, you can prevent that.

Related

Remove/Redirect public/index.php from url to prevent duplicate urls

Hello i am facing issue to remove the public/index.php from url. Remove/Redirect index.php from url to prevent duplicate urls
This link really helps me to remove index.php form url but i am not able to remove public/index.php from url. Here is my below htacces code
RewriteEngine On
#REmove index.php from url starts
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php[^/] /$1? [L,R=302,NC,NE]
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php(?:/(.*))?$ /$1$2? [L,R=302,NC,NE]
#Remove index.php from url ends
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
But when i add this below code to remove public/index.php in htaccess its not working :-
RewriteCond %{THE_REQUEST} /public/index\.php [NC]
RewriteRule ^(.*?)public/index\.php[^/] /$1? [L,R=302,NC,NE]
RewriteCond %{THE_REQUEST} /public/index\.php [NC]
RewriteRule ^(.*?)public/index\.php(?:/(.*))?$ /$1$2? [L,R=302,NC,NE]
Example urls that should be redirected:
mydomain.com/public/index.php/something should be redirected to mydomain.com/something (something could be anything - can contain any characters)
mydomain.com/public/index.php should be redirected to mydomain.com
mydomain.com/index.php?anything should be redirected to mydomain.com?anything (anything can contain any characters)
Pleae help me how to resolve this issue.
You may use this rule inside public/.htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} /public/index\.php(?:[/?](\S+))?\sHTTP [NC]
RewriteRule ^ /%1 [L,R=301,NE]
RewriteCond %{THE_REQUEST} /public/(\S*)\sHTTP [NC]
RewriteRule ^ /%1 [L,R=301,NE]
# other rules below this line
Additionally use this code in site root .htaccess:
RewriteEngine On
# remove /index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]
RewriteRule ^ public%{REQUEST_URI} [L]

mod_rewrite: don't rewrite any subdomains

Is there a way to exempt any requests to a subdomain in mod_rewrite? Right now, I have a one-page app that's redirecting everything to index.html.
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteRule (.*) index.html [L]
</IfModule>
But I want to make sure that calls to subdomain.mydomain.com are permanently exempt from being rewritten.
You can use a condition on the subdomain and the - substitution to stop the rewriting. From http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewriterule
There is a special substitution string named '-' which means: NO
substitution! This is useful in providing rewriting rules which only
match URLs but do not substitute anything for them.
Try
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.mydomain\.com$ [NC]
RewriteRule .* - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteRule (.*) index.html [L]

Using mod_rewrite with multiple Drupal installations

I have a website with Drupal installed in a subfolder /drupal. This instance needs to handle all URLs for the site, which it does successfully with the rules below:
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} !^131.216.164.200:10011$ [NC]
RewriteRule .* http://131.216.164.200:10011/ [L,R=301]
RewriteRule ^$ drupal/index.php [L]
RewriteCond %{DOCUMENT_ROOT}/drupal%{REQUEST_URI} -f
RewriteRule .* drupal/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* drupal/index.php?q=$0 [QSA]
However, I now need to add another Drupal folder, /otherdrupal, which will handle only URLs beginning with /something1 or /something2. What I've tried is modifying the rules to this:
RewriteCond %{HTTP_HOST} !^131.216.164.200:10011$ [NC]
RewriteRule .* http://131.216.164.200:10011/ [L,R=301]
RewriteRule ^(something1|something2)$ otherdrupal/index.php [L]
RewriteRule ^$ drupal/index.php [L]
RewriteCond %{DOCUMENT_ROOT}/drupal%{REQUEST_URI} -f
RewriteRule .* drupal/$0 [L]
RewriteCond %{DOCUMENT_ROOT}/otherdrupal%{REQUEST_URI} -f
RewriteRule .* otherdrupal/$0 [L]
But mod_rewrite is not gonna make it that easy...
(P.S. I'm aware of Drupal's multi-site feature, but assume that the second Drupal instance may be any web application that handles URLs the same way as Drupal does, e.g. with a q URL parameter.)
I figured it out. My working .htaccess file:
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} !^131.216.164.200:10011$ [NC]
RewriteRule .* http://131.216.164.200:10011/ [L,R=301]
RewriteRule ^$ drupal/index.php [L]
RewriteCond %{REQUEST_URI} ^(/something1|/something2)
RewriteRule .* otherdrupal/index.php?r=$1 [L,QSA]
RewriteCond %{DOCUMENT_ROOT}/drupal%{REQUEST_URI} -f
RewriteRule .* drupal/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* drupal/index.php?q=$0 [QSA]

mod_rewrite - friendly URLs and removing page extension

I need a little help with some mod_rewrite rules I am trying to use. I need to do 2 things
Change a dynamic URL so that it’s friendly (e.g. /content.php?CategoryID=1 change to /categories/1/)
Remove the .php extension on my pages (e.g. /page2.php to /page2)
Ok so I can get number 1 to work by itself with the following in my .htaccess file:
Options +Indexes
Options +FollowSymlinks
RewriteEngine on
RewriteOptions Inherit
RewriteBase /
RewriteRule ^category/([0-9]+)?$ category/$1/ [R]
RewriteRule ^category/([0-9]+)/?$ content.php?CategoryID=$1
I am then trying number 2 and I'm running into a few problems. I am using the following:
RewriteCond %{THE_REQUEST} \ /(.+/)?index\.php(\?.*)?\ [NC]
RewriteRule ^(.+/)?index\.php$ /%1 [NC,R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} -f
RewriteCond %{REQUEST_URI} ^(/.+)\.php$
RewriteRule ^(.+)\.php$ %1/ [R=301,L]
RewriteCond %{REQUEST_URI} ^(/.+)/$
RewriteCond %{DOCUMENT_ROOT}%1.php -f
RewriteRule ^.+/$ %1.php [QSA,L]
The above rerwrites the URL so it looks friendly (e.g. /page2/) but it hits my 404 page and I can't view it but my other page where I rewrite the URL still works (e.g. /category/1/).
This is my full .htaccess file:
Options +Indexes
Options +FollowSymlinks
RewriteEngine on
RewriteOptions Inherit
RewriteBase /
RewriteRule ^category/([0-9]+)?$ category/$1/ [R]
RewriteRule ^category/([0-9]+)/?$ content.php?CategoryID=$1
RewriteCond %{THE_REQUEST} \ /(.+/)?index\.php(\?.*)?\ [NC]
RewriteRule ^(.+/)?index\.php$ /%1 [NC,R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} -f
RewriteCond %{REQUEST_URI} ^(/.+)\.php$
RewriteRule ^(.+)\.php$ %1/ [R=301,L]
RewriteCond %{REQUEST_URI} ^(/.+)/$
RewriteCond %{DOCUMENT_ROOT}%1.php -f
RewriteRule ^.+/$ %1.php [QSA,L]
ErrorDocument 404 /misc/404page.html
Any help would be greatly appreciated as I am new to all this mod_rewrite and regex stuff.
I know it's been a while, and I'm not 100% sure what you were trying to do, but I would probably be looking at:
RewriteRule ^category/([0-9]+)$ category/$1/ [R,L]
RewriteRule ^category/([0-9]+)/$ content.php?CategoryID=$1 [L]
RewriteRule \.php? - [L]
RewriteRule ^(.+)\.php$ $1 [R,L]
RewriteRule ^([^/?])$ $1.php [L]
I'd need to look at your .htaccess more closely and/or have a better idea of the structure of your site to give better information.

Question about url rewriting using apache mod_rewrite

I have a doubt about url rewriting using apache mod_rewrite. I am a newbie in mod_rewrite and I don't have any experience in regex.
What I want to do is to:
Rewrite / To /web/content/public/
Rewrite /clients/ To /web/content/clients/
How can I achieve above things.
I tried:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^/clients/$ web/content/clients/ [L]
RewriteRule ^(.*)$ web/content/public/$1 [L]
</IfModule>
But it doesn't work. What can I do?
^(.*)$ includes the slash. So don't include the slash in the rewritten pattern.
But include a root slash at the head of your rewritten pattern.
RewriteRule ^/clients(.*)$ /web/content/clients$1 [L]
RewriteRule ^(.*)$ /web/content/public$1 [L]
Check the apache access log and error log to see what kind of request URL comes back.
Please check the documentation, especially the list labeled "Here are all possible substitution combinations and their meanings:"
Try this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^/clients/$ web/content/clients/ [L]
RewriteRule ^/(.*)$ web/content/public/$1 [L]
And if you want to use that rules in a .htaccess file, remove the leading slash from the patterns.

Resources