mod_rewrite to transform old request urll into new one - mod-rewrite

.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.

Related

301 URL Forwarding with HTACCESS or PHP

Just curious if anyone can help me on this HTACCESS issue.
I have these OLD URLS that need to get forwarded properly.
Previous structure
domain.com/Canada/Accounting
domain.com/Canada/Trades
domain.com/Canada/Sales
Proper structure
CATEGORY - /jobs/accounting-jobs
LOCATION - /jobs/jobs-kelowna
TOGETHER - /jobs/accounting-jobs-kelowna
Domain Structure
domain.com/jobs/[category]-jobs-[location]
Is this possible, either by HTACCES or PHP...just don't want these 404'ed pages.
I have 86+ to do, if there is a good way to forward these.
This is what I have, but i'm unable to successfully forward the bad-urls properly.
OLD
/browse
/Toronto/
/Canada/Administrative
/Vancouver/
/Canada/Trades
/Calgary/
/Canada/Hospitality
This is my HTACCESS right now.
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
#
# Trailing slash check
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(.*)$ $1/ [L,R=301]
#
# PAGES
RewriteRule ^add-job/?$ /add-job.php [L]
RewriteRule ^jobs/?$ /results.php [L]
RewriteRule ^sitemap/?$ /sitemap.php [L]
#
# SEARCH
# CATEGORY - accounting-jobs
# LOCATION - jobs-kelowna
# TOGETHER - accounting-jobs-kelowna
RewriteRule ^jobs/([a-zA-Z0-9_-]+)/([0-9]+)?$ results.php?whatwhere=$1&page=$2
RewriteRule ^jobs/([a-zA-Z0-9_-]+)/([0-9]+)/?$ results.php?whatwhere=$1&page=$2
To 301 redirect your pages you can do something like:
RewriteEngine On
RewriteBase /
RewriteRule ^(\w+)/(\w+)$ /jobs/$2-jobs-$1 [R=301,L]
This only addresses the urls from your previous structure (the combinations, you have not shown any previous urls with just location or category) but note that Canada will stay Canada, it does not become canada. You can change everything to lower case using rewrite as well.
You also have to take care that you don't rewrite any of the current urls but without more information, this should do it.
Edit: For the location-only urls you could use a rule like:
RewriteRule ^(\w+)/$ /jobs/jobs-$1 [R=301,L]
Again, you need to look out that your rewrite rule does not interfere with your current urls. If that is the case, you would need to redirect every old url manually.
For lower-case new urls, you should search SO, there are some questions with good answers about converting a mized-case variable to lower-case.
If you have mod_rewrite, you can add these lines to your .htaccess file:
RewriteEngine on
RewriteRule ^Canada/Accounting$ /jobs/accounting-jobs [R,L]
However, it's not clear from your question exactly what you want mapped. Are the 3 previous URLs supposed to redirect to the 3 new ones? They don't seem to be equivalent.

decoding mod_rewrite charactors

I'm attempting my first pretty url implementation via mod_rewrite. Just want to check if I'm on the right track. I'm doing it via dev environment.
I'm trying to get www.cysticlife.dev/Profile.php?id=34 to become www.cysticlife.dev/34/Profile
Would the regex mod_rewrite version then be:
RewriteEngine on
RewriteRule ^/([0-9]+)/?/Profile$ www.cysticlife.dev/Profile.php?id=$1 [L]
Thanks in advance.
RewriteEngine on
RewriteRule ^/([0-9]+)/Profile/index.html$ /$1/Profile [R=301,L]
RewriteRule ^/([0-9]+)/Profile/$ /$1/Profile [R=301,L]
RewriteRule ^/([0-9]+)/Profile$ www.cysticlife.dev/Profile.php?id=$1 [L]
The "?/" wasn't needed.
The lines I added makes both www.cysticlife.dev/34/Profile, www.cysticlife.dev/34/Profile*/* and www.cysticlife.dev/34/Profile*/index.html* work (with a 301 "Permanently moved" redirection so only one of the three urls is indexed by search engines).
Sidenote: You don't need to specify the full url for your rewrite. You could easily replace the last one with:
RewriteRule ^/([0-9]+)/Profile$ www.cysticlife.dev/Profile.php?id=$1 [L]

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

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).

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 and static pages

is possible to exclude a url being parsed by mod rewrite?
my .htaccess has rewrite rules like
RewriteRule ^contact contact_us.php
and a couple more static pages.
currently my site don't have troubles cause uses http://domain.com/user.php?user=username
but now i need rewrite to:
http://domain.com/username
I've tried with:
RewriteRule ^(.*)$ user.php?user=$1 [L]
but all my site stops working...
is possible to avoid parse my static pages like contact/feed/etc being treated like usernames?
edit to match david req:
this is my actual .htaccess file:
RewriteEngine On
Options +Followsymlinks
RewriteRule ^contact contact_us.php [L]
RewriteRule ^terms terms_of_use.php [L]
RewriteRule ^register register.php [L]
RewriteRule ^login login.php [L]
RewriteRule ^logout logout.php [L]
RewriteRule ^posts/(.*)/(.*) viewupdates.php?username=$1&page=$2
RewriteRule ^post(.*)/([0-9]*)$ viewupdate.php?title=$1&id=$2
RewriteRule ^(.*)$ profile.php?username=$1 [L]
also i've enabled modrewrite log my first file:http://pastie.org/1044881
Put the rewrite rules for the static pages first, and add the [L] flag to them:
RewriteRule ^contact contact_us.php [L]
...
then after those, use your rewrite rule for the username:
RewriteRule ^(.*)$ user.php?user=$1 [L]
(hopefully nobody has a username of contact).
EDIT: Based on the log output you posted (which I'm assuming corresponds to an unsuccessful attempt to access the contact page... right?), try changing the contact rewrite rule to either
RewriteRule ^contact$ contact_us.php [L]
or
RewriteRule ^contact contact_us.php [L,NS]
That is, either add $ to make the pattern match only the literal URL contact, or add the NS flag to keep it from applying to subrequests. According to the log output, what seems to have happened is that Apache rewrites contact to contact_us.php and then does an internal subrequest for that new URL. So far so good. The weird thing is that the ^contact pattern again matches contact_us.php, "transforming" it to contact_us.php, i.e. the same thing, which Apache interprets as a signal that it should ignore the rule entirely. Now, I would think Apache would have the sense to ignore the rule only on the subrequest, but I'm not sure if it's ignoring the entire rewriting process and leaving the original URL, /contact, as is. If that's the case, making one of the changes I suggested should fix it.
EDIT 2: your rewrite log excerpt reminded me of something: I'd suggest making the rewrite rule
RewriteRule ^([^/]+)$ user.php?user=$1 [L]
since slashes shouldn't be occurring in any usernames. (Right?) Or you could do
RewriteRule ^(\w+)$ user.php?user=$1 [L]
if usernames can only include word characters (letters, numbers, and underscore). Basically, make a regular expression that matches only any sequence of characters that could be a valid username, but doesn't match URLs of images or CSS/JS files.
The -f and -d options to RewriteCond check if the current match is a file or directory on disk.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ....

Resources