Can these two rewrite rules be written in a better way/merged? - mod-rewrite

I have the following rewrite rules:
RewriteRule ^(.*)/(stylesheets|javascript)/[0-9]+/(.*)$ /$1/$2/$3 [L]
RewriteRule ^(stylesheets|javascript)/[0-9]+/(.*)$ /$1/$2 [L]
it lets me do /foo/stylesheets/123456/bar.css which maps to /foo/stylesheets/bar.css and /stylesheets/123456/bar.css which maps to /stylesheets/bar.css
However I was just wondering if they could be merged somehow?
I tried changing it to just:
RewriteRule (stylesheets|javascript)/[0-9]+/(.*)$ $1/$2 [L]
but that didn't work for anything with /foo/stylesheets/
Thanksl

Try this one:
RewriteRule ^(.+/)?(stylesheets|javascript)/[0-9]+/(.*)$ /$1$2/$3 [L]
I'm just unsure how much performance it will give you (maybe even opposite).

Related

How To Rewriting URLs with 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.

mod_rewrite- matching everything after domain

This rule works fine for http://foo.com/page/contact
RewriteRule ^page/([^/\.]+)/?$ index.php?page=$1 [L]
But what I want to do is for http://foo.com/contact
But this is not correct:
RewriteRule ^/([^/\.]+)/?$ index.php?page=$1 [L]
How do I correct that?
Remove your first / that's not needed because the root directory is already there. like so:
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
your second example is technically asking for http://foo.com//contact

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: How to match file in directory or in root depending on 'availability'?

Hello, long time listener, first time caller here!
Thank you for the excellent advice you all share.
I have these mod_rewrite rules set up:
RewriteRule ^([^/.]+)/([^/.]+)/?$ $1--$2.php?%{QUERY_STRING} [L]
RewriteRule ^([^/.]+)/?$ $1.php?%{QUERY_STRING} [L]
They make /company/services/ redirect to company--services.php. Or /company/ to company.php. Works perfect.
But now I'd like to another rule that if I were to put services.php inside physical directory /company/ it will match and redirect that. And if failing that, look for my initial rule. (And failing that, return 404.)
I figured it would be as simple as including:
RewriteRule ^([^/.]+)/([^/.]+)/?$ $1/$2.php?%{QUERY_STRING} [L]
but not so. It will returns a 404 instead. I'm a bit stumped as this goes against how I believed mod_rewrite to work (if a rule does not match, go to the next one.)
Thank you for any pointers!
Found it. For reference, this is the complete set of rules:
RewriteCond %{DOCUMENT_ROOT}/$1/$2.php -f
RewriteRule ^([^/.]+)/([^/.]+)/?$ /$1/$2.php?%{QUERY_STRING} [L]
RewriteRule ^([^/.]+)/([^/.]+)/?$ $1--$2.php?%{QUERY_STRING} [L]
RewriteRule ^([^/.]+)/?$ $1.php?%{QUERY_STRING} [L]

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.

Resources