Well I say what's said in the title again, I can't find a way to rewrite all my urls without getting a loop.
I've tried many options but I can't find about one way which avoid a redirection loop:
RewriteRule ^/(.+) http://example.com/example/index.php$1 [R,L]
RewriteRule ^/?(.*) /var/www/example/index.php$1 [R]
RewriteRule ^/(.*)$ %{DOCUMENT_ROOT}/example/index.php$1 [R]
RewriteRule ^$ /example/ [L]
And below is my directory apache-conf
<Directory /var/www/example>
Options FollowSymLinks
DirectoryIndex index.php
Order allow,deny
Allow from all
</Directory>
I understand why it loops though I can't imagine (either find!) the good rule to do what I want.
EDIT
Basically I'm redirecting example.fr hosted at OVH to an IP virtual machine. Does this could get involved in my issue ?
Cheers
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/example/
RewriteRule (.*) /example/index.php/$1 [L,R]
This will redirect anything that is outside /example/ to /example/index.php adding the original path in the end
EDIT:
So, if you want everything to be redirected to /project_name/index.php instead, you need to swap the word "example" both in RewriteCond and in RewriteRule lines with your project name...
PS:
The RewriteCond line here is needed to ensure the rewriting does not loop (hence the url stayed /example/contact when you tried this).
This rule should not cause a loop
RewriteRule ^/(.+) http://example.com/example/index.php$1 [R,L]
This rule will not do anything at all
RewriteRule ^/?(.*) /var/www/example/index.php$1 [R]
For this rule you should add a RewriteCond
RewriteCond %{REQUEST_URI} !^/example/
RewriteRule ^/(.*)$ %{DOCUMENT_ROOT}/example/index.php$1 [R]
This is fine
RewriteRule ^$ /example/ [L]
Related
I would like to have two different redirects; one redirection if a user accesses an index.php-file on my apache-server and one redirection after a user enters a particular url.
So my index.php file lies in "/client/frontend/questionnaire"-folder on my apache. If a user enters "www.test-domain.com" he or she should be redirected to the index.php - file on the server.
The second redirection should be processed if a user enters "www.test-domain.com/news". Then he or she should be redirected to "www.test-domain.com/client/frontend/app/index.php/article-one".
I managed to create the first rewrite rule like this:
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^$ client/frontend/questionnaire/index.php [L]
But I do not know how to create the second rule.
Any help is very appreciated.
Thanks!
You have to use multiple RewriteCond+RewriteRule. See the mod_rewrite introduction and then the reference documentation (all is explain).
Just the RewriteRule actually rewrite the current request. To execute it, all RewriteCond before must be true.
Samples from the documentation:
RewriteCond %{REMOTE_ADDR} ^10\.2\.
RewriteRule (.*) http://intranet.example.com$1
RewriteCond %{REMOTE_HOST} ^host1 [OR]
RewriteCond %{REMOTE_HOST} ^host2 [OR]
RewriteCond %{REMOTE_HOST} ^host3
RewriteRule ...some special stuff for any of these hosts...
RewriteCond %{HTTP_USER_AGENT} (iPhone|Blackberry|Android)
RewriteRule ^/$ /homepage.mobile.html [L]
1) i am trying to 301 all non www's to www's (including files)
e.g. /subdirectory/ + /subdirectory/1.jpg (all possibilities with the www.)
2) i am trying to exclude ONLY but ALL .html and .php files from showing and would like only the non trailing version to be indexed, the rest to be 301's to my domain
e.g. /example.html or .php >> /example
e.g. /example.html or .php >> to not work + to not be indexed
e.g. /example to ONLY work and to ONLY be indexed (to avoid duplicate content)
3) i am trying to 301 all dead links and 404's to my domain
e.g. /deadlink or /deadlink.pdf >> 301'd to my domain, example.com
Here is the code i currently have, however i am not sure if it's 100% proper.
Can someone please reply with a validated syntax for these 3 tasks? Thanks.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . / [L,R=301]
OK, you've got three parts you're trying for here, and I'll address them all separately.
Before we begin, you only need to have one
RewriteEngine on
It only needs to be turned on once.
You should also set:
Options -MultiViews
as MultiViews can otherwise cause some weird issues here.
Redirect to WWW
Your current rules should work, but you can simplify them a little using the != prefix on RewriteCond:
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Removing .php and .html suffixes
You need two separate sets of rules here: one to remove suffixes from URLs, and another to readd them internally.
Let's do the second half first, since that's easier:
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [L]
RewriteCond %{SCRIPT_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
With this in place, though, removing suffixes is a little tricky without conflicting with the rule that goes ahead and tries to put them back on. The best approach I was able to come up with was:
RewriteCond %{THE_REQUEST} \.(html|php)
RewriteRule ^(.*)\.(html|php)$ $1 [R=301,L,NC]
But I'm open to suggestions.
301ing all dead links
Don't do that. If a link doesn't exist, it should return a page with a 404 status. If you want to redirect visitors to your home page from dead links, put a link in your 404 page.
I'm attempting to avoid urls like this one:
images/
or
images/valid-filename.png
or
images/invalid-filename.png
from being rewritten
and I've checked other example questions like .htaccess mod_rewrite on root directory but need it to skip all other directories and .htaccess mod_rewrite exemptions and this one https://stackoverflow.com/questions/7209746/can-mod-rewrite-skip-a-folder but I've not managed to get the rules to achieve what I'm after.
Here is what I have right now:
# Enable URL Rewriting
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(scripts|styles|images)(/.*|$) - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/robots.txt
RewriteCond %{REQUEST_URI} !=/favicon.ico
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# This solved the problem
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
RewriteCond %{REQUEST_URI} !images/.* [NC]
RewriteRule ^(.*)$ index.php [L,QSA]
</IfModule>
If I navigate to www.site.com/images/, I get redirected to www.site.com
If I navigate to www.site.com/images/invalid-filename.png, I get redirected to www.site.com
but www.site.com/images/valid-filename.png loads correctly.
Could someone help to explain how to achieve the rewrite behaviour described above?
Many thanks
Try adding this RewriteCond: RewriteCond %{REQUEST_URI} !images/.* [NC]
It will check whether the URI begins with images/ and if it does, the rule will not be met.
RewriteEngine On
RewriteRule ^/ai?$ /Market/publish.jsp [QSA,L,PT]
RewriteRule ^/ar?$ /Market/MailDispatch [QSA,L,PT]
RewriteCond %{HTTP_HOST} !^web\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://web.example.com/$1 [L,R]
#How skip www\. to web\. for this 1 ?
#RewriteRule ^/vi/?([0-9]+)\.htm$ /Market/vi.do?id=$1 [PT,L]
RewriteRule ^/li /Market/list.do [QSA,PT,L]
RewriteRule ^/vi/locations.jsp /Market/locations.jsp [PT,L]
ErrorDocument 404 /notfound.html
Nearly undoable(?) I try http://example.com/vi/{N}.htm should redirect to http://web.example.com/vi/{N}.htm where N is dynamic ID.
Seen mod_rewrite with subdomain and url pattern
There is no clear way to make eg http://example.com/vi/1096.htm pass up to next version http://web.example.com/vi/1096.htm where number is dynamic. I tried
A rule with the following scheme should do it:
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^/vi/\d+\.htm$ http://web.example.com%{REQUEST_URI} [L,R=301]
It’s important to put this rule in front of those rules that do an internal redirect. Otherwise an already internally rewritten URL could be rewritten externally.
If you want to use this rule in a .htaccess file, remove the leading slash from the pattern in RewriteRule.
I have a simple redirect which I just can't get to work and I don't know what's wrong. The server's throwing me a 500 Internal Server Error for no reason I can understand.
I'm trying to achieve the following: if a user types the address www.example.com, it will actually be routed to domain/ subdirectory in my server. So if user requests www.example.com/index.htm, it would fetch the file from /var/www/html/domain/index.htm.
Here's what I have so far:
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^(.*)$ domain/$1 [L]
Mod_rewrite is enabled and functional, as this does work:
RewriteRule ^(.*)$ domain/index.php?$1 [L]
What am I missing here?
You have to exclude the destination you want to redirect to:
RewriteCond %{SERVER_NAME} =www.example.com
RewriteCond $1 !^domain/
RewriteRule ^(.*)$ domain/$1 [L]
Otherwise you will get an infinite recursion since domain/… is also matched by .*.