Basic rewrite of url - mod-rewrite

I need a rule that rewrites
http://example.com/page.html?page=2
to
http://example.com/page.html/page/2
I already tried this rule:
RewriteRule ^(.*)page=(.*)$ http://example.com/$1/page/$2/
but this does not work! :( Where I'm wrong?

You can try this one:
RewriteCond %{QUERY_STRING} page=(\d+) [NC]
RewriteRule (.*) /$1/page/%1/? [R=301,L]
It should works.

Related

RewriteRule negate (NOT) not working

I am trying to do the following redirect:
any url such as:
/about/apple/1
/about/green
/about/apples
/about/pears/1
to:
/about/apple
using the following, which does not work:
RewriteRule ^about/(!apple) /about/apple [R=301,L]
The following test line works fine:
RewriteRule ^about/apples /about/apple [R=301,L]
I have tried:
RewriteRule ^about/!(apple) /about/apple [R=301,L]
RewriteRule ^about/(?!apple) /about/apple [R=301,L]
RewriteCond %{REQUEST_FILENAME} !(about/apple)
RewriteCond %{REQUEST_FILENAME} about(.*)
RewriteRule . /about/apple [R=301,L]
And also tried putting it into a rewritecond before hand, but nothing works.
Thanks.
Your regular expression pattern ^about/(?!apples) matches none of the uris you want to redirect. It matches /about/foobar but does not match /about/apples or /about/apples/foobar as you have negated the apples substring in the pattern.
You can use
RewriteRule ^about/apples/(.*)$ /about/$1 [L,R]
This will redirect /about/apples/ or /about/apples/foobar to /about/foobar .
Make sure to clear your browser cache before testing this.

RewriteCond %{REQUEST_URI} not working

I am trying to redirect all requests coming in to the web server as http://portal.company.com/legacy to http://portal.company.com/wps/portal/public/legacy/legacyportlet with the following rule, but it is not working as expected.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^portal\.company\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/legacy$ [NC]
RewriteRule ^(.*)$ /wps/portal/public/legacy/legacyportlet$1 [NC,L,PT]
I have also tried
RewriteCond %{HTTP_HOST} ^portal\.company\.com$ [NC]
RewriteRule ^/legacy /wps/portal/public/legacy/legacyportlet [NC,L,PT]
Any help would be greatly appreciated!
Thanks
It doesn't look like your source or target URLs change in any way, so possibly you're better off using Apache's basic Redirect directive which just redirects one URL to another.
Use this rule:
RewriteCond %{HTTP_HOST} ^portal\.company\.com$ [NC]
RewriteRule ^legacy/?$ /wps/portal/public/legacy/legacyportlet [NC,L]
Remember that in .htaccess RewriteRule doesn't match leading slash of URI.

Mod Rewrite won't redirect subdomains to new URL format

I'm changing all my subdomains to a single domains.
However, in order no to lose all my SEO I need to do some 301 redirections. My problem is that I have about 10.000 subdomains (it's a website about cities and each city is a subdomain) so I need to make a generic rewrite rule in order to make the new URLs (otherwise my htaccess will be too big).
I tried doing it myself but for some reason, it's doing what it wants to (so I guess I'm doing something wrong). Here is my code:
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com/b/^(.*)
RewriteRule ^(.*) http://domain.com/city/$1/b/$2 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteRule ^(.*) http://domain.com/?multi_city=$1 [R=301,L]
This is what happens with these two rules.
city.domain.com --> domain.com/?multi_city=/
city.domain.com/b/place --> domain.com/?multi_city=/b/place
What am I doing wrong?
Thanks in advance.
So, after many hours, I finally fixed it doing this:
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.(.*)
RewriteCond %{REQUEST_URI} ^/b
RewriteRule ^(.*)$ http://mydomain.%2/city/%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.(.*)
RewriteCond %{REQUEST_URI} ^/event
RewriteRule ^(.*)$ http://mydomain.%2/city/%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.com
RewriteRule ^(.*)$ http://mydomain.com/?multi_city=%1 [R=301,L]
This way I can redirect places and events first and if the URL is not in that format then it will go to the different format URL. It's probably not the most efficient solution but it works for me. Hope this helps to someone else.
I think the first RewriteCond it's wrong:
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com/b/^(.*)
The '^' symbol says that the string start, it's not part of the group, so I think that you will try:
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com/b/(.*)
Maybe, it will be better:
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com/b/([a-zA-Z0-9\-]+)
I'm doing it without testing, if it doesn't work I will make tests later and I'll answer you.

apache rewrite don't redirect?

apache .htaccess
RewriteRule ^promotion\.php\?do=content&id=38&mail$ promotion\.php?lang=tc&do=content&id=38 [R,L]
Thank you.
To match the query string, you have to use %{QUERY_STRING} like shown below.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^do=content&id=38&mail$ [NC]
RewriteRule ^promotion\.php$ promotion.php?lang=tc&do=content&id=38 [R,L]
Put it in the folder where promotion.php is present. I'm not sure how to involve RewriteCond ${HTTP_HOST} above.

Having trouble with mod rewrite non www to www

Can anyone help me with this:
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.$1 [R=301,L]
What I am trying to do is create a rewrite rule that sends you to the www version of a site if you try to connect using the non www version.
The condition works but the rules doesn't, it sends me to http://
Can anyone suggest hoe I can fix this.
I was expecting $1 = everything in the condition above between ^ and $
Thanks
Don't use HTTP_HOST, it's evil.
Do this:
RewriteCond %{SERVER_NAME} !^www\. [NC]
RewriteCond %{SERVER_NAME} (.*)
RewriteRule (.*) http://www.%1/$1 [R=301,L]
Where %1 matches the grouping from a previous RewriteCond.

Resources