Rewrite conditions in htaccess - .htaccess

I'm trying to create a sitemap for my multistore magento website. So each shop-view has it's own sitemap. Therefore I have made
sitemap/store_en/sitemap.xml
sitemap/store_de/sitemap.xml
sitemap/store_nl/sitemap.xml
What I'm trying to achieve is to redirect on request of mydomain.nl/sitemap.xml to mydomain.nl/sitemap/store_nl/sitemap.xml
This I have put in my htaccess file. But this doesn't work. Can anyone see what I'm doing wrong?
##rewrite rule for de sitemaps
RewriteCond %{HTTP_HOST} ^.*mydomain.nl$
RewriteRule ^sitemap.xml$ /sitemap/store_nl/sitemap.xml [NC]
I have another rewrite rule. I don't know if it is of any influence...
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{REQUEST_URI} !(.*)\.(html|shtml|php)$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [L,R=301]
Thanks in advance!

Directives for mod_rewrite are read and processed from the top to the bottom. In your case you had the redirect in the top. This rules matches (sitemap.xml is not a file, or a directory, does not end with a slash, html, shtml or php). You add an extra slash to it, and redirect the request.
Now a new request comes in for /sitemap.xml/. The first rule does not match (ends with slash) and the second rule does not match (request does not end with xml, but with xml/). You serve a 404 error.
When switching the rules, the url (/sitemap.xml) is matched against the first rule, and matches. It is now rewritten. On the second pass the first rule does not match, and the second rule does not match (valid file).

Related

How can a write correct rewrite rules in htaccess?

I havve been using an htaccess file to create rewrites so as not to not have to include .php/.html filenames in my urls.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example-page.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example-page.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /front /front_page.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /redirect /redirect-page.php
However as my code is currently written the redirects are not working and all attempts at loading the pages with anything else besides the filenames is giving me a 404 error. How might i fix my code to get the redirecting to work?
Write the directives like this instead:
RewriteEngine on
RewriteRule ^front$ /front_page.php [L]
RewriteRule ^redirect$ /redirect-page.php [L]
There doesn't seem to be a need for all your additional conditions? I assume you only have the one domain example-page.com and you don't have a file called /front etc.
In .htaccess, the URL-path matched by the RewriteRule pattern does not start with a slash. You should also include start/end-of-string anchors on the regex so that you only match front exactly and not match it anywhere in the URL-path.

Set up of conditional redirect in htaccess

I've been asked to make an existing web site multi-language.
In preparation for this I have had to move all existing pages from /path/page to /en/path/page
To maintain any existing incoming links I now need to set up an htaccess redirect to send any requests from their original urls to the new /en/path/page urls but I'm having trouble getting this to work.
This is what I currently have;
RewriteCond %{REQUEST_URI} !^/en$
RewriteRule ^(.*)$ /en/$1 [R=301,L]
Which I think is meant to check the requested URI and if it doesn't begin with /en then prepend /en onto the requested URI... but I'm obviously mistaken since it doesn't work.
Any help appreciated. Thank you.
UPDATE.
Since this is an ExpressionEngine site and there is an additional rule to remove the index.php portion of the URL here are both rules
# Rewrite for new language based urls
# This is to try and get all current pages going to /en/(old url) with a 301 redirect
RewriteCond %{REQUEST_URI} !^/en(/.*)?$
RewriteRule ^(.*)$ /en/$1 [R=301,L]
# Removes index.php
RewriteCond $1 !\.(gif|jpe?g|png|ico)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
I have also tried this with the language rewrite after the index.php one. I'm still getting stuck in loops.
What it does is, checking whether the URI is not exactly /en, since the $ indicates the end of the string right after en.
Try this, it checks whether the URI is not exactly /en or /en/ or doesn't start with /en/, and if that's the case it will prepend /en/:
RewriteCond %{REQUEST_URI} !^/en(/.*)?$
RewriteRule ^(.*)$ /en/$1 [R=301,L]
update Considering the other rules you have in your .htaccess file, it is necessary to have the language rule not match again for the following internal redirect to /index.php..., otherwise you'll end up with an endless loop.
There may be better ways to prevent this, however the first thing that comes to my mind would be checking for index.php in the first condition:
RewriteCond %{REQUEST_URI} !^/(index\.php|en)(/.*)?$
So this will cause the rule not to apply after the internal redirect. But be careful, this solves the problem for this specific case only in which the internal redirect goes to index.php!

htaccess - Remove file extensions and force subdomain

If a subdomain isn't in my URL (or its "www") I'm trying to force an underscore as the subdomain. I went round in circles yesterday trying to understand this, here's what I have so far:-
Rule #1.
Remove file extensions - works, until I add rule 2 to .htaccess.
Options -Multiviews
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
Rule #2
If there is no subdomain present automatically use an _ eg: _.mysite.com
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://_.%{HTTP_HOST}/$1 [L,R=301]
Rule 2 works, but it's adding the .php back onto the end of the filename?
I've tried moving the "_" rule above the extension rule but it doesnt make a difference.
Would be greatful for any help =)
To remove ".php" extension on second rule you have to add ".php" as a optional parameter:
RewriteRule ^(.*)(\.php)|(.*)$ http://_.%{HTTP_HOST}/$1$3 [L,R=301]
This way, no matter your URI have ".php" or not your request will always be redirected without ".php" extension.

Change a url via htaccess

I had a website which url is http://www.testingmyweb.comli.com
In my website a had a folder TCWEB. My website is in this folder.
I want to map http://testingmyweb.comli.com/TCWEB/home.html to http://testingmyweb.comli.com/
What can I changes do in .htaccess file?
Based on your comment, your htaccess file looks like this:
RewriteBase /
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
Redirect /TCWEB/home.html testingmyweb.comli.com
The rule that you have appends .html to requests that are for html files (but don't have the extension). The redirect that you have underneath that needs to be removed. In order for you to rewrite / to /TCWEB/home.html, that mod_alias directive will cause a loop. Replace it with these rules:
RewriteRule ^$ /TCWEB/home.html [L]
RewriteCond %{THE_REQUEST} /TCWEB/home.html
RewriteRule ^ / [L,R=301]
The first rule makes it so requests for / maps to /TCWEB/home.html, but the URL in the browser's address bar remains http://www.testingmyweb.comli.com. The second rule does what the Redirect does, except it checks that the request was actually made for /TCWEB/home.html, and not an internally rewritten URI. So when someone goes directly to /TCWEB/home.html, they get redirected to /, then the first rule gets applied.

htaccess and mod_rewrite using multiple querystrings

I am currently using htaccess to rewrite a single querystring to it's page name using the following and it's working fine...
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Rewrites urls in the form of /parent/child/
# but only rewrites if the requested URL is not a file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php?page=$1 [QSA]
My issue is that I am now trying to implement additional querystrings on top of the original. I can successfully access the page as /property-listing&property=12-Langworthy-Royston-Grove
However I would like to be able to rewrite the &property= to just be a forward slash, so essentially remove the additional querystring parameter, while maintaining the normal rewriting rules if no additional querystring parameters are passed.
Thanks for any help,
Matt.
Try adding this rule before last:
RewriteRule ^(.+)&property=(.*)$ /index.php?page=$1/$2 [L,QSA]
However note, that this will only work if &property= is part of URI (in the example you shown it is), because it is only the URI which is the subject of rewriting rule, not query-string.

Resources