I have a URL-structure like this:
mypage.com/search/f7_987_Aprilia/f8_1234_Aprilia+RS+125
mypage.com/search/f7_987_Aprilia
mypage.com/search/f8_1234_Aprilia+RS+125
And I need them to route internally (without redirection) to the following structure:
mypage.com/search?&f[7]=987&f[8]=1234
mypage.com/search?&f[7]=987
mypage.com/search?&f[8]=1234
I have come this far: /(f\d+)(\d+).*/
This gives me the desired IDs in the first set. But how do I get the parameters separated by slashes? And how do I "convert" to the desired structure?
Is that possible with htaccess?
I hope there is a genius out there who can come up with the desired mod_rewrite regex
Thank you in advance!
You may use this code in site root .htaccess:
Options -MultiViews
RewriteEngine On
RewriteRule ^search/([a-z])(\d)_(\d+)_[^/]*/?$ search?&$1[$2]=$3 [L,QSA,NC]
RewriteRule ^search/([a-z])(\d)_(\d+)_[^/]*/([a-z])(\d)_(\d+)_[^/]*/([a-z])(\d)_(\d+)_[^/]*/?$ search?&$1[$2]=$3&$4[$5]=$6 [L,QSA,NC]
Related
I have a .htaccess rule that goes like this
RewriteRule ^account/(.*) myaccount.php?p=$1 [NC,L]
This works fine for URLs like https://www.example.com/account/abcd.
However, now I want to pass query parameters to this URL, something like https://www.example.com/account/abcd?ab=1&cd=2.
Can't figure out the exact .htaccess rule that can accommodate this.
Any pointers?
Use:
RewriteRule ^account/(.*) myaccount.php?p=$1 [QSA,NC,L]
With QSA:
QSA|qsappend
When the replacement URI contains a query string, the
default behavior of RewriteRule is to discard the existing query
string, and replace it with the newly generated one. Using the [QSA]
flag causes the query strings to be combined.
http://httpd.apache.org/docs/current/rewrite/flags.html
Going to show my lack of regex knowledge here but seem to be having trouble with the following:
I need to redirect the following example structure of a url:
www.example.com/blog-title-here/tags/this+is+a+long+tag+name
www.example.com/tag/this-is-a-long-tag-name
I need to redirect to just /tag and replace any instance of + with a -
You can use this recursive redirect rule as your very first rule in site root .htaccess:
RewriteEngine On
RewriteRule "(?:.*/)?(tag/[^ +]*)[ +]+([^ +]*[ +].*)$" $1-$2 [N,NC,DPI]
RewriteRule "(?:.*/)?(tag/[^ +]*)[ +]+([^ +]*)$" /$1-$2 [L,R=301,NC,NE]
This will redirect http://localhost/blog-title-here/tag/this+is+a+long+tag+name to http://localhost/tag/this-is-a-long-tag-name
I have such a url:
/keyword1/keyword2/slugged-title-8286-1.jpg?wx=292&hx=164
I would like to forward in this case to:
/images/8286-1.jpg?wx=292&hx=164
the listing number (here 8286) can be 4 or 5 digits and could perhaps contain letters. Also the parameters after ? could be different.
Could you please help me to get this solved?
I haven't done a lot with regex and not sure how this can be done.
You can use this rule in your site root .htaccess:
RewriteEngine On
RewriteRule -(\w+(?:-\d+)?\.jpe?g)$ /images/$1 [L,NE,R=302]
If you don't want a full redirect then use:
RewriteRule -(\w+(?:-\d+)?\.jpe?g)$ /images/$1 [L]
QUERY_STRING is automatically carried over to target URL.
we recently moved to an addon domain. I'm having problems with the redirect. I need to redirect for the following situations:
mydomain.com/mysubfolder/ to mysubfolder.com
mydomain.com/mysubfolder/wp-login.php? to mysubfolder.com/wp-login.php?
mydomain.com/mysubfolder/page/ to mysubfolder.com/page/ [there are many pages]
This is my current regex:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/wp-login.php
RewriteRule ^/mysubfolder/(.*)$ http://www.mysubfolder.com/$1 [L,R=301]
It takes care of points 1 and 2. But I can't figure how to take care of point 3.
Thanks in advance.
Your regex redirects all pages except wp-login.php. It also does not account for the URL without the trailing slash. I added the ? to correct this.
RewriteEngine on
RewritRule ^/mysubfolder/?(.*)$ http://www.mysubfolder.com/$1 [L,R=301]
My goal is simply to redirect:
/jsn.php?parameters to http://www.site2.com/jsn.php?parameters
I tried with
Redirect permanent /jsn.php(.)* http://www.site2.com/jsn.php$1
Query string parameters are automatically passed, you simply want to do this:
Redirect permanent /jsn.php http://www.site2.com/jsn.php
The (.)* doesn't work with the Redirect directive, you were probably thinking of RedirectMatch, but either way, you don't need it. And also (.)* should be (.*), otherwise the $1 backreference would only get the first character.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^(jsn\.php)$ http://www.site2.com/$1 [L,NC,R=301]
You can use an explicit URL rewrite in your .htaccess file:
RewriteRule ^/jsn\.php\?(.*) http://www.site2.com/jsn.php?$1 [R]
Note: You need to escape . and ? because they are also regular expression characters.
If you have a problem using mod_rewrite, post the contents of your file.