optional second directory in htaccess redirect - .htaccess

Hi
im wanting to write a mod redirect which handles the following:
www.domain.co.uk/brands
rewrites to www.domain.co.uk/index.php?p=brands
www.domain.co.uk/brands/5
rewrites to www.domain.co.uk/index.php?p=brands&go=5
Can this be achieved in one line without a conditional statement?
I have written this, but the second line is ignored:
RewriteRule ^(.*)\.html$ index.php\?p=$1 [L]
RewriteRule ^((.*)/*)(.*)\.html$ index.php?p=$1&go=$2 [L]
Any help would be much appreciated

Well, it seems to me that the first rule would match both versions, and rewrite www.domain.co.uk/brands/5 to www.domain.co.uk/index.php?p=brands/5 -- and then the [L] flag makes the matching stop. It wouldn't match the second rule after the rewrite, anyway.
The second regexp has an asterisk too many (after the slash) and one set of parens too many as well, but if you fix that and move it above the other one, it might help.

Just reverse the order. As a general practice you must have most specific rules first and most generic as last. Try this in your .htaccess:
RewriteRule ^([^/]*)/(.*)\.html$ /index.php?p=$1&go=$2 [L,NC,QSA]
RewriteRule ^(.*)\.html$ /index.php?p=$1 [L,NC,QSA]
This will redirect URI of '/brands/5.html' to /index.php?p=brands&go=5 and a URI of '/brands.html' to /index.php?p=brands

RewriteRule (.*)(/(.*))?$ index.php?p=$1&go=$3 [L]
should do it.
The reason your second rule fails is, you are matching one character (.) followed by a / at the start of the string, so immediately any urls with more than one character before the first slash will fail. You are also insisting that the url ends with html but in your examples they don't. Also for the future, remember . matches any single character so you probably meant to escape the . before html.

Related

Redirect /blog/article to /articles/article in .htaccess

As the title says, I want to redirect from my old blog URL to the new one. Here is what I tried:
RewriteRule ^/blog/(.*)$ /articles/$1 [R=301,NC,L]
I really thought that this would work but it turns out that I am missing something. If you could help out that would be great.
The URL-path that is matched by the RewriteRule pattern in a per-directory context (ie. in .htaccess) never starts with a slash because the directory-prefix (that ends with a slash) is first removed. In order words, the regex ^/blog/(.*)$ will never match.
You would need something like the following instead:
RewriteRule ^blog/(.*) /articles/$1 [R=301,NC,L]
Note the absence of the slash at the start of the pattern. This contrasts when mod_rewrite is used in a server (or virtualhost) context, then the slash prefix is required.

Two rules in one .htaccess file not working

Below is my code for .htaccess
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ /products/product-full-view.php?src=$1&id=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/?$ /products/product-full-view.php?src=$1&id=$2
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ /buy/buy-full-view.php?src=$1&id=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/?$ /buy/buy-full-view.php?src=$1&id=$2
First rule is working fine but its not taking the second rule...Unable to understand what is happening here...
Original URL's are like this
www.example.com/products/product-full-view.php?src=somevalue&id=somevalue
and for second one
www.example.com/buy/buy-full-view.php?src=somevalue&id=somevalue
Please help me to run second rule also.
Thanks in advance
You're trying to match the same pattern, give or take an optional trailing slash, four times. By the time you reach the last two rules, your URL is already rewritten to something else.
You probably want something that looks more like:
RewriteEngine On
RewriteRule ^products/([0-9]+)/?$ /products/product-full-view.php?id=$1 [L]
RewriteRule ^buy/([0-9]+)/?$ /buy/buy-full-view.php?id=$1 [L]
Or:
RewriteEngine On
RewriteRule ^products/([a-zA-Z0-9_-]+)/([0-9]+)/?$ /products/product-full-view.php?src=$1&id=$2 [L]
RewriteRule ^buy/([a-zA-Z0-9_-]+)/([0-9]+)/?$ /buy/buy-full-view.php?src=$1&id=$2 [L]
Or something to that order anyway.
Note the stub and the [L] each time: the products/ and buy/ avoid that the same URL refers to two different locations, and the [L] (for "Last") tells the rewrite engine to stop processing rules when it gets matched.
If a URL matches the second rule, it also matches the first rule. After applying the first rule, the resulting URL no longer matches the second rule. That's why the second rule is never applied.

Mod rewrite to redirect except on a certain page

Trying to write a rewrite rule in my htaccess so that any request to /en/shop/index.html or /fr/shop/index.html stays on the server, but if the user goes to any other page it redirects to a different server. Here's what I've got so far and it doesn't work.
RewriteRule ^(.*)/shop/(.*) [L]
RewriteRule ^(.*)$ http://www.newwebsite.com/$1 [R=301]
Add a dash to tell the first RewriteRule that you want the matches to be passed through unchanged:
RewriteRule ^.*/shop(/.*)?$ - [L]
I also removed the first set of parentheses since you're not using the results of the match so there's no need to store the matched patterns. I assumed you might need to match /shop without a trailing slash so that's why the (/.*)? section is there.

mod_rewrite - check for string

I want to check if a URL contains the sting "-EN.htm", if so apply the rewrite.
That should be done with ^-EN.htm as follows, but the rule is not working:
RewriteCond %{REQUEST_URI} ^/(.*?)/([-_0-9a-zA-Z./=]*)^-EN.htm
RewriteRule ^(.*)$ /indexEN.php?folder=%1&follow=%2 [L]
What am I doing wrong?
Thank you for every help,
Scott
Your regular expression doesn't look right. You can also lose the condition and just move the pattern to the rewrite rule instead. Something along the lines of
RewriteRule ^/?(.*?)/([-_0-9a-zA-Z./=]*)^-EN.htm /indexEN.php?folder=$1&follow=$2 [L]
You need to make the leading slash optional (in htaccess this is stripped off) and instead of using % backreferences, use the $ ones.
Now on to your pattern, it's not valid. The ^ matches the beginning of the string (the URI), so if you have two of them and you're not trying to literally match the ^ character (which you'd need to escape), then the expression will never match anything. Without any examples of URLs that you're having to deal with, I assume you probably just want to ditch the second ^:
RewriteRule ^/?(.*?)/([-_0-9a-zA-Z./=]*)-EN.htm /indexEN.php?folder=$1&follow=$2 [L]

htaccess redirect with php variables ...

I'm using this format in my htaccess to redirect several pages/links:
RewriteEngine On
RewriteRule special.php http://www.mysite.com [R=301]
...
...
RewriteRule http://www.mysite.com/special.php?t=master http://www.mysite.com/index.php?q=former [R=301, L]
I noticed, first, that only the top line is catching anything, and in fact the others, like the bottom line, did nothing until I put in that top line. Any ideas why?
Second, mysite.com/special.php?t=grave is redirected, by the above top line, to mysite.com/?t=grave , thus retaining the variables in the URL. I don't want this, I simply want it to go to mysite.com with no variables. How do I do this?
Thanks,
Derek
First, your first rule catches any URI with special.php in it, even if it is followed by a bunch of characters. To limit it to only and exactly special.php, and to make sure the query string is discarded, change it to
RewriteRule ^special.php$ http://www.mysite.com/? [L, R=301]
Secondly, rewrite rules only match the part after http://www.mysite.com/ (note the last slash) and before the query string (the part after the question mark). So if you change the format of those rules to
RewriteCond %{QUERY_STRING} t=master
RewriteRule ^special.php$ index.php?q=former [R=301, L]
you should be good to go.

Resources