mod_rewrite is being too strict - mod-rewrite

I have a rewrite rule that is too strict.
RewriteRule ^folder/([^/\.]+)/?$ file.php?$1 [L]
This redirects http://www.domain.com/folder/$variable to http://www.domain.com/file.php?$variable HOWEVER.. if the variable has any dots or slashes it fails. I want it to redirect to http://www.domain.com/file.php? even if $variable is empty or contains any characters

Change ([^/\.]+) to (.*?).
The first matches at least one of anything except dots and forward slashes. The second matches 0 or more of anything, up to the posibility of a trailing slash (because of the /?$ at the end of your regex).

Related

Rewrite rule 301 with different formatting

Okay, so I need to find the correct syntax for what I want to do.
These links:
http://www.example.com/sub1/sub2/product1/
(...)
http://www.example.com/sub1/sub2/product700/
need to redirect to
http://www.example.com/sub3/product1-newsite/
(...)
http://www.example.com/sub3/product700-newsite/
What I have tried:
RewriteEngine On
RewriteRule ^/sub1/(.*)/(.*)$ http://www.example.com/subdir/$1-newsite/ [R=301,L]
For all the 700 products.
Also I need to make exeptions for certain products.
Can someone see what I'm doing wrong?
I think it's just the numbering on the regular expression that is an issue.
$1 is the first matched group, $2 is the second, etc. $0 is for the entire string that matched (ie. the whole path).
Try:
RewriteEngine On
RewriteRule ^/sub1/sub2/(.*)$ http://www.example.com/subdir/$1-newsite/ [R=301,L]
or
RewriteEngine On
RewriteRule ^/sub1/(.*)/(.*)$ http://www.example.com/subdir/$2-newsite/ [R=301,L]
Edit
Ah... had a thought... the first regex in the capture may be being a bit too greedy... since it can match:
^/sub1/[sub2/product1-newsite]/[] so $1 will be sub2/product1-newsite and $2 will be empty (.* can happily match nothing).
So we need to make sure it doesn't grab too much.
RewriteEngine On
RewriteRule ^/sub1/([^/]+)/(.+)/$ http://www.example.com/subdir/$2-newsite/ [R=301,L]
Changing the * to + makes sure we match at least one character.
Changing the . to [^/] makes sure we match "anything except a /".
Added a / on the end to make sure the $2 doesn't capture the trailing slash.
That should hopefully fix it.

accept parentheses as a character in mod_rewrite

i have to redirect some old page to new page i used below commend in .htaccess
RewriteRule ^Savane-98.htm$ savane-tc-9347-41-belgian-tapestry-throw [R=301,L]
it works fine but some of my old link have parentheses '()' it don't work how can i solve that . like below link
RewriteRule ^antique-bronze-square-end-rod-(flat-to-wall)-tapestry-wall-hanging-rod$ antique-bronze-square-end-rod-flat-to-wall-tapestry-rod [R=301,L]
this link don't work can anyone help me
Thanks
Parentheses are special characters in regular expressions - they denote the beginning and end of a matching group.
In order for them to be treated as literal parentheses by the regular expression engine, they need to be escaped with a backslash (\):
RewriteRule ^antique-bronze-square-end-rod-\(flat-to-wall\)-tap...

Trying to write a mod_rewrite rule

I've changed my CMS and need to write a mod_rewrite rule to help redirect some of the old URLs.
What I'd like to do is:
remove "blog/archives"
replace underscores with dashes
replace ".html" with a trailing slash
The old link:
http://example.com/blog/archives/the_post_title.html
The new link
http://example.com/the-post-title/
To address 1 & 3 I thought something along the lines of this might work, but it's not.
RewriteRule ^/blog/archives/([A-Za-z0-9-]+)/?.html$ $1 [L]
Thanks for your suggestions.
For 1 and 3
RewriteRule ^/blog/archives/(.*?).html$ /$1/ [L,R=permanent]
(note that R=permanent use a 301 redirect, which will be cached for long time but does move your pagerank to the new URL. Use [L,R] to use normal redirection)

Explain this rewrite rule?

I have managed to put this condition and rule below together to redirect a page to include index.shtml at the end of it but I dont understand what this part of the Rewrite means - the ^$ part. I believe ^ means start and $ is end but in this context I dont understand its meaning in "English" and how it works:
RewriteCond %{HTTP_HOST} ^www\.example1\.test\.com [NC]
RewriteRule ^$ http://www.example1.test.com/index.shtml [R,L]
Thanks.
^ is the start of the string, $ is the end. If there's nothing in between, as in the case of ^$, that means an empty string. Since the path leading up to the current directory is ignored, the part of the URL that matches is after the http://www.example1.test.com/. Matching an "empty string" after that means matching the URL itself, as in: with no file or anything else specified. So, this redirects the root URL to index.shtml
If a user visits the exact URL www.example1.test.com he or she is redirected to http://www.example1.test.com/index.shtml.
Useful tool: http://martinmelin.se/rewrite-rule-tester/

why doesn't this rewrite the url properly?

RewriteCond %{REQUEST_URI} !^/?new.php?url
RewriteRule ^/?(.+)\.?(.+)$ new.php?url=$0 [L]
its supposed to take any URL
mysite.com/someurl
and convert it to
new.php?url=someurl
however it keeps going to just new.php
You need to escape the second question mark in the first line so it matches a literal question mark:
RewriteCond %{REQUEST_URI} !^/?new.php\?url
Also you are not using the parenthesized groups on the second line. $0 is okay, or you may want $1 instead. If you use $0 you could simplify it a bunch:
RewriteRule ^.*$ new.php?url=$0 [L]
Or on the other hand if you're breaking apart the URL for a reason I would suggest some fixup. You're not matching the file name and extension exactly right. A little more complex regex like this would probably do you better:
RewriteRule ^/?(.*?)(?:\.([^.]*))?$ new.php?path=$1&extension=$2 [L]
Explanation:
(.*?) matches the directory and file name. The ? means match non-greedily, so stop as soon as the next part matches. The parentheses cause it to be captured and stored in $1.
(?:\.([^.]*))? matches the file extension. ?: turns says to not capture the outer set of parentheses, so the dot is not captured in $2. ([^.]*) matches the extension and ensures that it does not contain a dot. The final ? makes the file extension part optional, just cause not all URLs have file extensions. Thus there will only be a $2 if there is a file extension.
The first back-reference is $1 not $0 AFAIK. If that doesn't do it try specifying [QSA] as well, though I doubt that's it.
RewriteCond %{REQUEST_URI} !^/?new.php?url
RewriteRule ^/?(.+)\.?(.+)$ new.php?url=$1 [L]
The first back reference should be $1 instead of $0.
RewriteCond %{REQUEST_URI} !^/?new.php?url
RewriteRule ^/?(.+)\.?(.+)$ new.php?url=$1 [L]
It also depends on what other lines of code you have in the file. It's also possible to mess up rewrites if you have another code that conflicts with it.

Resources