I have a regular expression for a framework to convert host.com/controller/method in to host.com/index.php?controller/method
Like this:
RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ /index.php/$1 [L]
I now require it to convert into this,
host.com/controller/method -> host.com/index.php?controller=$1&method=$2
How would I do a match with a regular expression like so within htaccess?
Something like this?
RewriteRule ^(\w+)/(\w+)$ /index.php?controller=$1&method=$2 [L]
If you just want one path segment instead of just anything, use [^/] instead of .:
RewriteCond $1 !=index.php
RewriteRule ^([^/]*)$ /index.php/$1 [L]
And for your second requirement:
RewriteCond $1 !=index.php
RewriteRule ^([^/]*)/([^/]*)$ /index.php?controller=$1&method=$2 [L,QSA]
Here you should also consider to use the quantifier + (one or more) instead of * (zero or more).
Related
I would like to rewrite everything that does not start with wp-content but ends in .html. My failed attempt:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/wp-content.*
RewriteRule ^(.*)\.html /$1-pages/ [R=301,L]
You don't need RewriteCond here. Just modify next rule to:
RewriteRule ^/?(?!wp-content/)(.*)\.html$ /$1-pages/ [R=301,L]
I used a negative lookahead to negate the results.
I have hundreds of these old links I need to redirect.
Here is one example:
/index.php?option=com_content&view=article&id=433:seventh-character-code-categories-and-icd-10-cm&Itemid=101&showall=1
to
/seventh-character-code-categories-and-icd-10-cm
Essentially I need to remove the /index.php?option=com_content&view=article&id=433: part.
I tried this but I am getting confused with the [0-9] and : parts, so the following does not work:
RewriteRule ^/index.php?option=com_content&view=article&id=[0-9]:(.*)$ /$1 [L,R=301]
Say you want to capture from after : to right before & in the query string you mentioned, then try this expression:
^[^\:]*\:([^\&]*)\&.*$
As #starkeen mentioned in comments, you got to check against the query string. This can be done using RewriteCond %{QUERY_STRING}
So if index.php is in the root folder:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^\/index\.php$
RewriteCond %{QUERY_STRING} ^[^\:]*\:([^\&]*)\&.*$
RewriteRule ^(.*)$ http://example.com/%1 [R=301,L]
Here's another example. This one is for a sub folder:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^\/pages\/index\.php$
RewriteCond %{QUERY_STRING} ^[^\:]*\:([^\&]*)\&.*$
RewriteRule ^(.*)$ /pages/%1? [R=301,L]
Also, notice the ? at the end of the url /pages/%1?, this prevents from re-attaching the query string.
Another thing, captured groups will be set to variables %{number} since set in the RewriteCond.
BTW, depending on your server's configuration, you may need to add the NE flag, like [NE,L,R=301] Plus test whether it is necessary to double escape the literal characters.
what is about direct approach. Skip all till semicolon, mach string till & and replace all with first much
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} [^:]+:([\w-]+[^&]).*
RewriteRule .*$ \/%1? [R=301,L]
</IfModule>
So I'm trying to write a rule that will respond with a 404 if certain strings are passed to any of the php scripts. Here's with what I came up with:
RewriteBase /
RewriteCond %{QUERY_STRING} ^.*(string1|string2).*$ [NC]
RewriteRule ^$ [R=404,L]
That rule appears to be matching only www.domain.com/?string1 or www.domain.com/?String2, but not www.domain.com/whatever.php?var=string1 or www.domain.com/directory/script.php?var=string1 or www.domain.com/directory/1/script.php?var=string1 and so on.
Can anyone help and point out what I am doing wrong?
Best,
-Iulian
Your RewriteRule is requiring an empty path. Try it like this:
RewriteBase /
RewriteCond %{QUERY_STRING} ^.*(string1|string2).*$ [NC]
RewriteRule ^.*$ - [NC,L]
As Kevin says, you are requiring an empty URL before the query string, with ^$. You don't need all the .*, you don't have to match the full string. This will work, you don't need the RewriteBase either:
RewriteCond %{QUERY_STRING} (?:string1|string2) [NC]
RewriteRule ^ - [R=404,L]
The ?: just says don't capture this, it's only for grouping. The ^ is a way of matching anything. The - says don't change the URL.
Having this regexp in my .htaccess:
RewriteRule ^thumbnails/([0-9]*)/([0-9]*)/(.*)$ lib/thumb.php?w=$1&h=$2&src=$3 [QSA]
I'm having an issue when passing an url in arguments. The regexp remove all slashes but one. Example:
Enter: domain.com/thumbnails/200/143/http://img.youtube.com/vi/xxxxxxx/0.jpg
Result: domain.com/lib.tuhmb.php?w=200&h=143&src=http:/img.youtube.com/vi/xxxxxxx/0.jpg
Note there is just one slash after http:.
Any ideas?
Thanks!
That is an expected behavior in mod_rewrite since rewrite engine strips multiple / into single / while applying pattern in RewriteRule.
To overcome this behavior use RewriteCond %{REQUEST_URI} to capture your values like this:
RewriteCond %{REQUEST_URI} ^/thumbnails/(\d+)/(\d+)/(.*)$ [NC]
RewriteRule ^ lib/thumb.php?w=%1&h=%2&src=%3 [L,QSA]
I have links like these that I want to change:
mypage.com?page=missions&id=5
mypage.com?page=hello
I tried to change them into easier links with this:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /index.php?page=$1&id=$2 [L]
It works but if I want to access pages like (mypage.com?page=hello) I have to write:
mypage.com/hello/
and if I write without the slash in the end like this
mypage.com/hello
it doesn't work.
How do I fix it? :)
This should work:
RewriteRule ^([^/]*)(/([^/]*))?$ /index.php?page=$1&id=$3 [L]
This will make the slash optional by including it in an optional group (denoted by (...)?), along with the optional second half of the query string. Since this introduces a new group between the first and second (left parenthesis determines the order), we have to change the second backreference from $2 to $3.
If the logic becomes much more complex than this, it may be easier to split up the rules.
You could add a second rule that omits the second parameter and optionally the slash:
RewriteRule ^([^/]+)/?$ /index.php?page=$1
RewriteRule ^([^/]+)/(\d+)/?$ /index.php?page=$1&id=$2
This might work too:
RewriteRule ^([^/]+)(?:/(\d+))?/?$ /index.php?page=$1&id=$2
For SEO, you'll probably want to redirect requests missing the slash to the same address with a slash. Use RedirectMatch for that.
I read about the trailing slash with SEO (didn't know about it, thank you mathew!) and the final result was this:
RewriteRule ^([a-zA-Z0-9_-]+)/$ /index.php?page=$1 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ /index.php?page=$1&id=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ http://www.mypage.com/$1/ [R=301,L]
So now I force it to have a trailing slash.