I'm setting up a working copy of a site, and copied the entire site to a mamp. But I have problem setting up a working version of .htaccess.
Working directory on localhost is "folder1".
This is the live .htaccess:
RewriteEngine On
RewriteBase /
# Rewrite site site.eu to site.eu/en/
RewriteCond %{QUERY_STRING} !language=(sv|en|ru|jp)
RewriteRule ^$ en/ [L,R=301]
# Add trailing slash if necessary
RewriteRule ^(sv|en|ru|jp)$ $1/ [R=301,L]
# Add language part to old bookmarks/links
RewriteCond %{REQUEST_URI} !^/(sv|en|ru|jp)/
RewriteCond %{QUERY_STRING} !language=(sv|en|ru|jp)
RewriteRule (sale|brands|latest_in_stock)(.*) en/$1$2 [R=301,L]
# Remove www
RewriteCond %{HTTP_HOST} ^www\.site\.eu$ [NC]
RewriteRule (.*) http://site.eu/$1 [R=301,L]
RewriteRule ^(sv|en|ru|jp)/(.*)$ $2?language=$1 [L,QSA]
RewriteRule ^sale sale.php [L,QSA,NC]
RewriteRule ^brands/([^/]+) /designer.php?idnr=$1 [L,QSA,NC]
RewriteRule ^brands /labelList.php [L,QSA,NC]
...
On the local copy I changed to:
RewriteBase /folder1/
I tried to removed the part with "Add language code to old... " and the part "Remove www".
I also tried different changes to the last RewriteRule for exampel adding "folder1" and remove it from RewriteBase.
RewriteRule ^brands /folder1/labelList.php [L,QSA,NC]
The php-files and the index-file is working fine, but none of the /brands for example. I get a 500 error, too many redirects.
The variable %{REQUEST_URI} will contain the part behind the domain name, regardless of which .htaccess file it is in. What will happen is the following:
We start with localhost/en/brands/asdf. It matches the following rule:
RewriteCond %{REQUEST_URI} !^/(sv|en|ru|jp)/
RewriteCond %{QUERY_STRING} !language=(sv|en|ru|jp)
RewriteRule (sale|brands|latest_in_stock)(.*) en/$1$2 [R=301,L]
The first condition is true, because ^/(sv|en|ru|jp)/ does not match /folder1/en/.... The second condition is true, because we haven't reached the rule yet that sets the language in the query string. Finally it will match the last one, because (sale|brands|latest_in_stock)(.*) will match en/brands/asdf ($1 will contain brands and $2 will contain /asdf). This will redirect to localhost/folder1/en/brands/asdf after re-adding the directory-prefix.
You have several options:
1. adding folder1 to the condition
If you add folder1 to the condition with %{REQUEST_URI}, this problem does not occur:
RewriteCond %{REQUEST_URI} !^/folder1/(sv|en|ru|jp)/
2. adding a negative look-ahead
If you add a negative look-ahead to the rule, and remove the condition, things work out correctly, because the first argument of RewriteRule will match against what you expect. The pro of this is that you do not need to know in which directory this is:
#Remove this: RewriteCond %{REQUEST_URI} !^/(sv|en|ru|jp)/
RewriteCond %{QUERY_STRING} !language=(sv|en|ru|jp)
RewriteRule ^(?!(sv|en|ru|jp)/)[^/]*/?(sale|brands|latest_in_stock)(.*) en/$1$2 [R=301,L]
You might need to alter the capture group references. I can't test them here.
3. Moving rules around
Moving the following rule:
RewriteRule ^(sv|en|ru|jp)/(.*)$ $2?language=$1 [L,QSA]
just above the rule I just pointed out should theoretically solve this problem. I would strongly recommend against this solution, because intermixing redirects and rewrites makes it very hard to read.
Related
I'm trying to use mod_rewrite to simplify my website structure but it is proving difficult.
I need any exact matches of example.com/inventory/ to redirect to example.com/#search/ (I have this working).
I need to have any urls that match example.com/inventory/(.*) to load normally and not be redirected to /#search/ (I have this working).
I need to have example.com/#search/(.) redirect to example.com/inventory/$1 except in the following cases:
-example.com/#search/order-miles/
-example.com/#search/order-year/
-example.com/#search/order-price/ example.com/#search/page-(0-9)+/ -example.com/#search/makemodel-(.)/
Here is what I have so far:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^inventory/(.*)$
RewriteRule ^(.*)$ https://example.com/inventory/$1 [R,L]
RewriteCond %{REQUEST_URI} ^#search/(.*)$
RewriteCond %{REQUEST_URI} !^#search/(order-miles|order-year|order-price|makemodel)$
RewriteRule ^(.*)$ https://example.com/inventory/$1 [R,L]
RewriteCond %{REQUEST_URI} ^inventory$
RewriteRule ^(.*)$ https://example.com/#search/ [R,L]
It appears this is all working except for the second set of conditions and corresponding rule.
/#search/order-miles/ loads properly (and the other 3 subfolder structures), but the other urls, such as:
https://example.com/#search/nissan-frontier/9017/nissan-frontier/
should redirect to
https://example.com/inventory/nissan-frontier/9017/nissan-frontier/
and does not.
Does anyone see a better way to do this?
I have written this little piece of code. I am very new to this so i am not sure it is all correct. but basically it lets me access urls with the php extension. When people get on the site they are being redirected from the geo ip page to the correct language which like looks like this
main.php?lang=uk or nl or en or eu etc.
right now i can also use it like this
main/?lang=uk or nl or en or eu etc.
I would like to be able to to also remove the variable in the url ?lang=uk.
How would i do this. My .htaccess code is below
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
DirectorySlash On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1 [R=301,L]
# remove trailing slash
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
RewriteRule ^([\w\/-]+)(\?.*)?$ $1.php$2 [L,T=application/x-httpd-php]
</IfModule>
Thanks too anyone willing to help.
The first argument of RewriteRule does match anything after the domain name and prefix* and before any query string if that exists. In http://localhost/this/is/my/test?lang=123 with a .htaccess file in the this/is/ directory it would match my/test. To match a query string, you have to use the %{QUERY_STRING} variable.
If the second argument (the rewritten url) of RewriteRule does not contain a query string, it will automatically append the query string of the original url to the new url.
In the code below I use %{THE_REQUEST}. This is the string that is used to make the request for a page. It is in the form of GET /my/resource?query=string HTTP/1.1. It does not change when rewriting things, which makes it useful to prevent infinite loops.
In your php file make sure that the language is read from a cookie instead of a get variable, then do the following:
#Set cookie for language
RewriteCond %{QUERY_STRING} ^(.*?)&?lang=([^&]+)&?(.*?)$
RewriteRule ^ %{REQUEST_URI}?%1&%3 [CO=lang:%2:127.0.0.1:1:/:0:1,R,L]
#Remove potential prefix or suffix & from query string
RewriteCond %{QUERY_STRING} ^&(.*?)$ [OR]
RewriteCond %{QUERY_STRING} ^(.*?)&$
RewriteRule ^ %{REQUEST_URI}?%1 [R,L]
#External requests with \.php should be without that.
RewriteCond %{THE_REQUEST} \.php
RewriteRule ^(.*)\.php$ $1 [R=301,L]
#Try to load php page if resource does not alreay have an extension
RewriteCond %{REQUEST_URI} !\.[a-z]+$
RewriteRule ^(.*)$ $1.php [L]
If want to redirect all non-www requests to my site to the www version. All I need to do is add the following code to my .htaccess file.
RewriteCond %{HTTP_HOST} ^mydomain\.com [NC]
RewriteCond %{REQUEST_URI} !^/subfolder
RewriteRule .* http://www.mydomain.com%{REQUEST_URI} [R=301,L]
The problem is that when I write for example mydomain.com/products-1 (hidden URL for mydomain.com/products?category=1), all parameters become visible, even though they are specified on the .htaccess file, and I get an output url (after the redirect) of www.mydomain.com/products-1?category=1
How can I fix this? Is there any kind of problems with the .htaccess code above?
Try Changing your RewriteRule:
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{REQUEST_URI} !^/subfolder
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
I prefer this because it will catch all *.domain.com. If that is not what you want, then use your original HTTP_HOST rule.
If my logic is working this morning, this rule should rewrite any requests that do not match:
www.example.com
and do not contain
/subfolder
to
www.domain.com/URI
Basically I want to rewrite my urls so that it is website.com/folder/ sometimes though I need it to rewrite also website.com/folder/page/
Currently I have it working with just the website.com/folder/ but can not get it to check if there is a page, if I create just another rule under the folder one it reads that one, and gives me an empty page var, which is breaking my php. I struggle with .htaccess and any help would be appreciated.
Here is what I have that works with just the folder but I can not include a page.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/?(css|js|images|html|docs)/
RewriteRule ^([^/]*)/$ /?folder=$1 [QSA]
Here is what I tried to get it to work with either just a folder, or a folder and page
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/?(css|js|images|html|doc)/
RewriteRule ^([^/]*)/$ /?folder=$1 [QSA]
RewriteCond %{REQUEST_URI} !^/?(css|js|images|html|doc)/
RewriteRule ^([^/]*)/([^/]*)/$ /?folder=$1&page=$2 [L,QSA]
Please Help!
Accordingly to the RewriteRule docs you should reverse the rules order in your rules set. Because in your configuration both rules have the same RewriteCond, the most specific rule (folder + page) should be atop and the most general rule should be the last one. If not when the first rule is matched the URL is rewritten and the second rule never matches. Also, probably you want to remove the trailing forward slash in the pattern of your folder + page rule (assuming that the second group in the pattern matches a page not a folder). So I think the whole thing should read:
RewriteCond %{REQUEST_URI} !^/?(css|js|images|html|doc)/
RewriteRule ^([^/]*)/([^/]*)$ /?folder=$1&page=$2 [L,QSA]
RewriteCond %{REQUEST_URI} !^/?(css|js|images|html|doc)/
RewriteRule ^([^/]*)/$ /?folder=$1 [L, QSA]
In my infinity stupidity I changed the permalinks on my wordpress blog a little while back and have now changed them again. The problem that I have now caused is that I have a few hundred URL's out there which no longer work.
For example this URL looks like the ones that used to work
http://www.lazygamer.net/the-evopoints-co-za-downloads-of-the-week-1305/
But you'll see that just gives you a 404 not found page now because my site expects the first subdirectory to be a category such as
http://www.lazygamer.net/xbox-360/the-evopoints-co-za-downloads-of-the-week-1305/
So now I want to put a htaccess rule in that checks to see if a category exists and if it doesn't then just add in something random to make the url resolve.
I'm pretty sure I can do this with a regular expression of sorts but I can't figure it out.
[Update] My current .htaccess file
RewriteEngine On
RewriteBase /
RewriteRule ^/([^/]+)/$ /category/$1/ [R]
RewriteRule ^index\.php$ - [L]
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^www.lazygamer\.co.za$ [NC]
RewriteRule ^(.*)$ http://www.lazygamer.net/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^lazygamer\.co.za$ [NC]
RewriteRule ^(.*)$ http://www.lazygamer.net/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^lazygamer\.net$ [NC]
RewriteRule ^(.*)$ http://www.lazygamer.net/$1 [R=301,L]
Read up mod_rewrite to understand how to use these for other problems!
RewriteRule ^/([^/]+)/$ /category/$1/ [R]
That should do it.
Beware, this will redirect anything with one directory path in the URL to /category/{original_url_path}