.htaccess rewriterule for folder - .htaccess

I'm trying to redirect using the link: http://www.mysite.com/af/2 to http://www.mysite.com/af/2/, but I can not perform this procedure, the link /af/#### corresponds to a user ID and is a folder.
You can accomplish this using .htaccess?

A rule like the following will take care of that:
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^ %{REQUEST_URI}/ [R=301,L]
The condition is to prevent urls that already end with a slash to match this rule. The rule will match everything ^ and redirect it to the same url with a slash behind it.

Related

htaccess redirect if parameter exists not working

I'm trying to rewrite any URL if a special parameter exists.
So that this happens:
From: www.example.com/somepage/someother/?entryValue=somevalue
To: www.example.com/somepage/someother/?product=test&special=12345ls&linkSource=website
I tried to following, but it doesnt work as expected:
This code adds var/www/* instead of the link
www.example.com/var/www/web6/htdocs/example.com/index.php/*
RewriteCond %{QUERY_STRING} (^|&)entryValue=somevalue
RewriteRule ^(.*)$ $1/?product=test&special=12345ls&linkSource=website [L,R]
This code removes the path:
RewriteCond %{QUERY_STRING} (^|&)entryValue=somevalue
RewriteRule ^(.*)$ /?product=test&special=12345ls&linkSource=website [L,R]
How can I make it work?
RewriteCond %{QUERY_STRING} (^|&)entryValue=somevalue
RewriteRule ^(.*)$ $1/?product=test&special=12345ls&linkSource=website [L,R]
You need to include a slash prefix at the start of the substitution string. Like this:
RewriteRule ^(.*)$ /$1/?product=test&special=12345ls&linkSource=website [L,R]
Without the slash prefix (the URL-path matched by the RewriteRule pattern does not include a slash prefix) it is seen as relative and the directory-prefix (ie. /var/www/...) will be added back and result in the malformed redirect you are seeing.
UPDATE:
but this ends up with "index.php" and the path is lost
You've put the directive in the wrong place and have a conflict. The order of the mod_rewrite directives is important.
Generally, external redirects like this need to go near the top of the .htaccess file, before any internal rewrites (like a front-controller).

Prevent redireting for a specific URL

I'm redirecting if the url contains a specific word.
RewriteRule ^forum(/.*)?$ /homepage.html?no=%{QUERY_STRING} [R=301,L,NE,NC]
Is it possible to avoid redirecting for a specific url that has the above specific word?
E.g.
The below gets redirect
http://www.exsample.com/forum/contactus.html?ypin9001234
to
http://www.exsample.com/homepage.html?no=ypin9001234
What I need is to continue the above redirect as it is but to avoid the redirect only for the below URL.
http://www.exsample.com/forum/members/gold/gold.html?ypin9001234
Note: The word gold is used only by this url.
You should use a RewriteCond with THE_REQUEST to match full URL with query string:
RewriteCond %{THE_REQUEST} !/forum/members/gold/gold\.html\?ypin9001234 [NC]
RewriteRule ^forum(/.*)?$ /homepage.html?no=%{QUERY_STRING} [R=301,L,NE,NC]
You can add a rewrite condition before your rewrite rule, e.g.:
RewriteCond %{REQUEST_URI} !^.*gold.*$
RewriteRule ^forum(/.*)?$ /homepage.html?no=%{QUERY_STRING} [R=301,L,NE,NC]
This will prevent any url containing the word gold from being rewritten. If you need to be more specific, just make the condition more precise:
RewriteCond %{REQUEST_URI} !^forum/members/gold.*$
RewriteRule ^forum(/.*)?$ /homepage.html?no=%{QUERY_STRING} [R=301,L,NE,NC]
etcetera...

Rewrite conditions in 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).

htaccess rule for remove begining part of the url

I need to hide beginning part of the url. For example
Url will be http://bookoffers.com?id=http://www.google.com
Where id will be another url. So I want to show only the value of the id. And hide "http://bookoffers.com?id=" this much part.
Try adding this rule on top of other rules:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+\?id=(https?://[^\s]+) [NC]
RewriteRule ^ %1 [R=301,L,NE]

.htaccess redirect - two URLs with similar paths

So here's what I have.
www.website.com/foo (pretty URL to use on marketing pieces)
www.website.com/foobar (URL that actually exists on site)
I can get www.website.com/foo working perfectly with this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /foo [NC]
RewriteRule ^ http://www.website.com/redirected/url-goes-here/ [L,R=301]
But that makes the www.website.com/foobar URL go there as well.
I'm sure this is a regex issue and I just don't know the correct symbol to get things working properly, but how can I make /foo redirect properly without effecting /foobar ?
Thanks.
Try this instead:
RewriteCond %{REQUEST_URI} ^/foo$ [NC]
RewriteRule ^ http://www.website.com/redirected/url-goes-here/ [L,R=301]
REQUEST_URI will get rid of the extra request headers that THE_REQUEST has. Then you can match the beginning and end of the requested URL with ^ and $.
You don't need the RewriteCond. Just be specific with the RewriteRule pattern
RewriteRule ^foo$ http://www.website.com/redirected/url-goes-here/ [L,R]
See more about regular expression.
Never test with 301 enabled, see this answer Tips for debugging .htaccess rewrite rules for details.

Resources