decoding mod_rewrite charactors - mod-rewrite

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]

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.

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.

URL Rewriting invisibly - How to prevent rewritten URL to appear in the address bar?

I have browsed the other topics, including this one: Mod_rewrite invisibly: works when target is a file, not when it's a directory, but I can't find a solution to my problem.
I have the following rewriting rule:
RewriteRule ^([a-zA-Z0-9_-]+)$ ./index.php?s=$1 [L,NC]
RewriteRule ^([a-zA-Z0-9_-]+)/$ ./index.php?s=$1 [L,NC]
What it does is to write anything like http://myaddress/parameter to http://myaddress/index.php?s=parameter and show this new rewritten address in the browser's address bar.
How can I make rewriting without showing the rewritten URL in the address bar?
Edit
This is the content of my .htaccess file:
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^([a-z0-9_\-]+)/?$ index.php?s=$1 [L,NC,QSA]
RewriteRule ^([a-zA-Z0-9_-]+)\/?([a-zA-Z0-9_-]+)\/?$ index.php?u=$1&s=$2 [L,NC]
RewriteRule ^([a-zA-Z0-9_-]+)\/?([a-zA-Z0-9_-]+)\/?([a-zA-Z0-9_-]+)\/?$ index.php?u1=$1&u2=$2&s=$3 [L,NC]
1. No need for 2 rules that do the same job (the only difference is presence of trailing slash).
2. No need to have a-zA-Z in pattern if you have [NC] flag -- a-z is enough.
3. Try rule without ./
Considering all the above mentioned the rule will become:
RewriteRule ^([a-z0-9_\-]+)/?$ index.php?s=$1 [L,NC,QSA]
P.S.
I have also added the QSA flag to preserve original query string (if present).
The rule is tested and is working fine. If it still does not work for you then post ALL rewrite rules that you have.
This should work:
RewriteEngine On
RewriteRule ^([-a-zA-Z0-9_]+)$ index.php?s=$1 [L]
RewriteRule ^/?$ index.php [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.

mod_rewrite - redirect from one domain to another and preserve trailing values in url

I think this is a pretty straight forward question in mod_rewrite:
I got one domain, which needs to redirect to another, but keep any value after last slash (/) in the first URL, over to the second.
domain.com/4433 should transfer to domain.com/folder/?p=4333
Listed for clarity:
From: domain.com/4433
To: domain.com/folder/?p=4333
Any ideas?
Edit:
Did some testing, we found the following solution:
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^([0-9a-z]*)$ /folder/?p=$1 [NC]
sincerely,
- bakkelun
In case you don't really want to redirect but to have pretty URLs, you can use
RewriteEngine On
RewriteRule ^/(.+)$ /folder?p=$1 [L]
This takes everything after the first slash and inserts it at the $1 - but only if there's something after the slash. It doesn't issue a redirect so the users won't notice.
Without any further information, try this:
RewriteEngine on
RewriteRule ^/([^/]+)$ /folder/?p=$1
If you want to use the rule in a .htaccess file, remove the leading slashes.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$ domain.com/folder?p=$1 [R=301,L]
Just in case: domain.com = domain1.com and domain2.com? domain1.com should be redirected to domain2.com? Both run on the same server (optional)?
[EDIT:]
If you really only want to do the thing as stated in the comment, then do the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1.com$
RewriteRule ^4433$ http://domain2.com/folder/?p=4433 [R=301,L]
Else, as Benedikt Eger said, or with R=301 if you want real redirection.
Or, if you want it to redirect only on numbers, then do the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1.com$
RewriteRule ^([0-9])+$ http://domain2.com/folder/?p=$1 [R=301,L]
RewriteCond checks, if defined vhost is domain1.com, but not domain2.com, then the rewrite rule is applied, and redirects via HTTP status 301 [R=301] only number strings (0-9)+ consisting of at least one number to the specified URL. [L] makes this the last rule applied.

Resources