How To Rewriting URLs with Mod-Rewrite - mod-rewrite

How would I rewrite:
http://mydomain.com/?v=service
http://mydomain.com/?v=pfolio
To
http://mydomain.com/service
http://mydomain.com/pfolio
I am have tried many ways but have not figured it out. Please give a suggestion.
I already use
RewriteRule ^/?v=(.*)$ /v/$1 [R=301,L]

This would be the version for the server configuration:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^v=(.*)$
RewriteRule ^/$ /%1 [R=301,L]
Or, if there might occur more query parameters something like this:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^v=([^&]*)
RewriteRule ^/$ /%1 [R=301,L,QSA]
When placed inside a .htaccess style file you have to remove the / from the pattern in the RewriteRule (so just ^$).
But I am skeptical what this really is what you are really looking for. Usually people want to do the opposite of this... Maybe you should also explain what is the idea behind your attempt. So your situation, what you want to happen, not how.

Related

Rewriterule not applied in Apache 2.4 httpd.conf

I would like to redirect via a RewriteRule (mod_rewrite) enabled in httpd.conf my URL:
https://mysite.domain.tld/index_php_file.php?ab=ident_keys&ac=5GU7VBNAH45DA5
TO:
https://mysite.domain.tld/index_php_file.php?ab=ident_key_1024&ac=5GU7VBNAH45DA5
I have tried it with a number of rules without luck:
RewriteCond %{HTTP_HOST} hmysite.domain.tld
RewriteRule ^/index_php_file\.php\?ab=ident_keys&ac=$ https://hmysite.domain.tld/index_php_file.php?ab=ident_key_1024&ac= [R=301,L,QSA]
nor
RewriteCond %{QUERY_STRING} ^ac=(.*)$
RewriteRule ^/?([a-z-0-9-_.]+)$ /$1/index_php_file.php?ab=ident_key_1024&ac=%1 [L,R=301]
seems to rewrite the URL.
Any suggestions on what I'm missing?
Thank you very much.
I found the solution, it may help someone.
RewriteCond %{QUERY_STRING} ac=([A-Z0-9]+)
RewriteRule ^/?([-a-zA-Z0-9_+]+)$ index_php_file.php?ab=ident_key_1024&ac=$1 [R=301,L]
It is enough to look for the string (containing UPPERCASE chars and numbers only) with the RewriteCond and rewrite the URL RewriteRule to the desired format and append the value of the variable from the query.
Do not forget to enable the mod_rewrite module within Apache. To take effect a restart is also necessary of course.

How can I use mod_rewrite to do this

I would like to take a url at my site:
http://mysite.com/jk/drawing
but operationally drop the "jk" dir and have my users see this:
http://mysite.com/drawing
Is this possible? If so can someone give me an example of how it is done?
thanks,
The first thing you need to do is go and change all of your links from looking like this: http://mysite.com/jk/drawing to looking like this: http://mysite.com/drawing. Without doing this, people will still see all the /jk/ URLs everywhere, the only thing you can do about it is to make sure you've changed all your links. Then add these rules to the htaccess file in your document root:
RewriteEngine On
RewriteCond %{HTTP_HOST} mysite.com [NC]
RewriteCond %{REQUEST_URI} !^/jk/
RewriteRule ^(.*)$ /jk/$1 [L]
In order to correct for all the links still pointing to /jk/ that you don't have any control over:
RewriteCond %{HTTP_HOST} mysite.com [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /jk/([^\ ]*)
RewriteRule ^ /%1 [R=301,L]

Mod_rewrite Help

This is what I have, but it is not working.
RewriteRule ^location/([a-zA-Z0-9_-]+)/reviews$ location.php?purl=$1&page=reviews [L]
RewriteRule ^location/([a-zA-Z0-9_-]+)/reviews/$ location.php?purl=$1&page=reviews [L]
Can anyone show me what I am doing wrong?
http://www.example.com/this-location-1234/reviews/ to http://www.example.com/location.php?purl=this-location-1234&page=review
You should combine those two (nearly identical) rules, and make sure you have the RewriteEngine on and your rule is being run (either your .htaccess is getting picked up, or you have this in an active vhost definition).
RewriteEngine on
RewriteRule ^location/([a-zA-Z0-9_-]+)/reviews/?$ location.php?purl=$1&page=reviews [L]
I fixed it. I accidentally was missing some verbiage in the link I was getting an error on.
RewriteRule ^location/([a-zA-Z0-9_-]+)/reviews/?$ location.php?purl=$1&page=reviews [L]
This works great!
I think this should work for the example URLs you given:
RewriteRule ^([a-zA-Z0-9_-]+)/reviews/?$ location.php?purl=$1&page=reviews [L]
(replace the two rewrite rules with this one)
If you don't already have one, add RewriteEngine on before this RewriteRule.
If you actually wanted to rewrite /location/this-location-1234/reviews/ instead of /this-location-1234/reviews/, then use this:
RewriteRule ^location/([a-zA-Z0-9_-]+)/reviews/?$ location.php?purl=$1&page=reviews [L]
I assume you have another property in your conf file called RewriteEngine on ?
Also, you can turn on the logging with RewriteLogLevel.

mod_rewrite to transform old request urll into new one

.hello - i need to transform old url requests to fit into the new sites content;
ie 'art-consultancy' used to be 'consultancy' so how can i grab 'consultancy' urls and transform them into 'art-consultancy'
MY RULE if ^consultancy$ MAKE ^art-consultancy$ and continue to the rules below...
RewriteRule ^art-consultancy$ consultancy-02.php [L]
RewriteRule ^art-consultancy/$ consultancy-02.php [L]
RewriteRule ^art-consultancy/([a-zA-Z0-9\-]+)$ consultancy-02.php?section=$1 [L]
RewriteRule ^art-consultancy/([a-zA-Z0-9\-]+)/$ consultancy-02.php?section=$1 [L]
#
RewriteRule ^art-consultancy/([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)$ article-01.php [L]
RewriteRule ^art-consultancy/([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/$ article-01.php [L]
any help appreciated!
ps. tried Redirect /consultancy /art-consultancy without any luck. Think this method needs an actual file?
best, Dc
You basically just have to do exactly what you said you wanted to do, in a similar way that you've done with the other rules, so I'm not sure how much this qualifies in the way of an "answer"...But, for the sake of completeness, I'll go ahead and write up the full thing:
(Also, I condensed your other rules into single lines)
# Add in this condition because consultancy-02.php matches here too
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^consultancy(.*)$ art-consultancy$1
RewriteRule ^art-consultancy/?$ consultancy-02.php [L]
RewriteRule ^art-consultancy/([a-zA-Z0-9\-]+)/?$ consultancy-02.php?section=$1 [L]
RewriteRule ^art-consultancy/([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/?$ article-01.php [L]
If you wanted consultancy to be transformed to art-consultancy in the user's browser URL, you should replace the first RewriteRule with this:
RewriteRule ^consultancy(.*)$ /art-consultancy$1 [R=301,L]
I'm not entirely sure why the Redirect didn't work like you expected it to though. If you look at your server's error_log it might tell you, but otherwise it's hard to speculate without knowing what your site's directory structure looks like.

mod_rewrite: hierarchical sub-domains into hierarchical sub-directories

Consider the following problem, please. I have got domain.tld with hierarchical sub-domains like the following:
a.domain.tld
b.a.domain.tld
c.b.a.domain.tld
... etc.
There is also hypothetical directory structure in the web-root:
/foo
/a.
/a./b.
/a./b./bar
/a./b./c.
... etc.
I would like to achieve such rewrite that would cause Apache to serve directories in a way you see below.
a.domain.tld --> /a.
b.a.domain.tld --> /a./b.
c.b.a.domain.tld --> /a./b./c.
... etc.
Directories without trailing dot character would behave as normal sub-directories.
domain.tld/foo/ --> /foo
a.b.domain.tld/bar --> /a./b./bar
I can not use mod_vhost_alias and would be glad if the solution was achievable using mod_rewrite only. Is it possible to achieve such rewrite?
Thank's for all your advices.
--
nkd
Possible solution for 4 levels of sub-domains:
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.tld
RewriteRule (.*) %1./$1 [R,L]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.domain\.tld
RewriteRule (.*) %2./%1./$1 [R,L]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)\.domain\.tld
RewriteRule (.*) %3./%2./%1./$1 [R,L]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)\.([^\.]+)\.domain\.tld
RewriteRule (.*) %4./%3./%2./%1./$1 [R,L]
Thank you.
--
nkd
The previous solution ends in very funny infinite redirect loop. Here's a solution I got now (not very elegant, but it works; but with a huge 'but'):
# Working solution for five levels of sub-domains
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^\.]+)\.DOMAIN\.TLD [NC,OR]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.DOMAIN\.TLD [NC,OR]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)\.DOMAIN\.TLD [NC,OR]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)\.([^\.]+)\.DOMAIN\.TLD [NC,OR]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)\.([^\.]+)\.([^\.]+)\.DOMAIN\.TLD [NC]
RewriteRule (.*) http://DOMAIN.TLD/%5./%4./%3./%2./%1./$1 [R,L]
Can somebody explain to me why (the hell) it works? It really does work, I tested it extensively. But why does it work actually? If I look at the RewriteRule line I doubt it should work... Thank you for your explanations.
BTW: If the above five rewrite conditions and rule work, it looks like it could be re-written in some sort of 'two-liner' (one condition plus one rule). I tried that already, by using the above rule and the following condition instead of the five conditions given above:
RewriteCond %{HTTP_HOST} ^(([^\.]+)\.)+DOMAIN\.TLD [NC]
and played with it a little but with no real success. Thanks for all ideas how to simplify
the rewrite stuff and make it more 'sane' (if possible).
--
nkd
It is possible if you send the part to be reversed to an external script via RewriteMap and have that handle it.

Resources