Same RewriteCond and multiple RewriteRule - regex

I've got in my application some review rules here's example:
RewriteCond %{HTTP_HOST} ^(.*)-street\.
RewriteRule ^(.*)/(.*)\.html$ index.php?street=%1&$1=$2 [L]
RewriteCond %{HTTP_HOST} ^(.*)-street\.
RewriteRule ^(.*)$ index.php?street=%1 [L]
I expect if I type: example-street.example.com/type/param.html that it use this rule:
RewriteRule ^(.*)/(.*)\.html$ index.php?street=%1&$1=$2 [L]
but unfortunately it use less specified one.
What should I add/change in my code to get expected result?
EDIT
I want to achive this result:
example-street.example.com/type/param.html -> index.php?street=example&type=param
example-street.example.com/whateverelse -> index.php?street=example

Check this, hope it will work fine.
Check .htaccess here
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/type/param.html$
RewriteCond %{HTTP_HOST} ([a-z]+)\-([a-z]+)\.example\.com$
RewriteRule ^([a-z]+)\/([a-z]+)\.html$ /index.php?%2=%1&$1=$2 [R=301,L,QSA]
RewriteCond %{REQUEST_URI} ^/[a-z]+$
RewriteCond %{HTTP_HOST} ([a-z]+)\-([a-z]+)\.example\.com$
RewriteRule ^.*$ /index.php?%2=%1 [R=301,L,QSA]

Related

.htaccess RewriteRule with two different conditions

I have two different URLs:
1.: mysite.com/file.php
2.: mysite.com/**articles**.php?article=**something**
I would like to rewrite mysite.com/file.php to mysite.com/file.
AND
I would like to rewrite mysite.com/**articles**.php?article=something to mysite.com/**news**/**something**
I tried this but it's not working.
It rewrite mysite.com/file.php to mysite.com/file, but does not rewrite mysite.com/**articles**.php?article=**something** to mysite.com/**news**/**something**
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^?\ ]+)\.php
RewriteCond %{REQUEST_FILENAME} !exception.php
RewriteRule ^news/([^/]+)(/*)$ /articles.php?article=$1
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
(There is one exception in the code: exception.php which I don't want to rewrite.)
Can you please correct my code? I've been working on this for 4 days, I've read everything, but I can't do it.
With your shown samples and attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect for file.php file.
RewriteCond %{REQUEST_URI} !/?exception\.php [NC]
RewriteCond %{THE_REQUEST} \s/(file)\.php/?\s [NC]
RewriteRule ^ /%1? [R=301,L]
##Internal rewrite for php files.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ $1.php [QSA,L]
##external + internal rewrite rules for articile.php here.
RewriteCond %{THE_REQUEST} \s/articles\.php\?article=(\S+)\s [NC]
RewriteRule ^ /news/%1? [R=301,L]
RewriteRule ^news/([^/]*)$ articles.php?article=$1 [NC,QSA,L]

Apache Wildcard Rewriting Subdomain to a Sub Directory

Been trying to Rewrite wildcard subdomain to a subdirectory and not having allot of luck.
ie
abc.domain.com > www.domain.com/sub/abc
I have tried the folowing
// example 1
RewriteCond %{HTTP_HOST} ^(.*).domain.com [NC]
RewriteRule ^(.*)$ ./sub/$1 [L]
// example 2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(.*).domain.com [NC]
RewriteRule ^(.*)$ ./sub/$1 [L]
Hope someone can advise.
You need to use backreference from RewriteCond:
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteRule ^((?!(?:sub|index\.php)/).*)$ ./sub/%1/$1 [L,NC]

Removing GET parameter in .htaccess

I'm not able to remove one GET parameter wrongly added by one 404 SEF module.
I want to remove "task=view" only if it appears alone and not with another parameter.
So
www.mysite.com/1.html?task=view should be redirected to www.mysite.com/1.html.
While www.mysite.com?task=view&view=article should remain unchanged.
No matter which RewriteRule I use, this paramater does not go. Looks like it is being generated from any environment variable when index.php is run.
For example this does not work:
RewriteCond %{QUERY_STRING} ^task=view$ [NC]
RewriteRule (.*)task=view.* $1 [R=301,L]
Here is my .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]
RewriteCond %{QUERY_STRING} base64_encode.*(.*) [OR]
RewriteCond %{QUERY_STRING} (<|%3C).*script.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|[|\%[0-9A-Z]{0,2})
RewriteRule ^(.*)$ index.php [F,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|.php|.html|.htm|.feed|.pdf|.raw|/[^.]*)$ [NC]
RewriteRule (.*) index.php
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
Options -Indexes
What is the way to handle this?
Use this rule:
RewriteCond %{QUERY_STRING} ^task=view$ [NC]
RewriteRule ^(.*)$ $1? [R=301,L]
? at the end of $1 will strip-off any existing query string.

Rewrite subdomain exactly like the main website

I'm currently working on a blog portal, and I'm trying to achieve a specific thing here.
This is my .htaccess code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.[NC]
RewriteCond %{HTTP_HOST} ^(.+?)\.MyDomain.com$ [NC]
RewriteRule ^$ blogdisplay.php?page=%1 [L,QSA]
RewriteRule ^([a-zA-Z0-9-]+)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9-]+)/$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)$ index.php?page=$1&page2=$2
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/$ index.php?page=$1&page2=$2
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)$ index.php? page=$1&page2=$2&page3=$3
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/$ index.php?page=$1&page2=$2&page3=$3
When a person is trying to reach SOMETHING.mydomain.com, htaccess is rewriting it to blogdisplay.php?page=BLOGNAME. Just as planned. Now, the problem is that I want it to behave exactly like the main website and rewrite SOMETHING.mydomain.com/page to blogdisplay.php?page=BLOGNAME?page2=page.
Is there any way to make the subdomain use blogdisplay.php instead of index.php, but otherwise work exactly like the subpages of the "main website"?
Replace your code with this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.[NC]
RewriteCond %{HTTP_HOST} ^(.+?)\.MyDomain.com$ [NC]
RewriteRule ^$ blogdisplay.php?page=%1 [L,QSA]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f
## don't do anything
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.+?)\.MyDomain\.com$ [NC]
RewriteRule ^([a-z0-9-]+)/?$ blogdisplay.php?page=%1&page2=$1 [L,QSA]
RewriteRule ^([a-z0-9-]+)/?$ index.php?page=$1 [L,NC]
RewriteRule ^([a-z0-9-]+)/([a-zA-Z0-9-]+)/?$ index.php?page=$1&page2=$2 [L,NC]
RewriteRule ^([a-z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ index.php?page=$1&page2=$2&page3=$3 [L,NC]

url rewriting in .htaccess

I am trying to serve different urls using mod_rewrite but whatever I try it is just not working.
An example url would be
http://www.site.com/country/tours/dynamic-part/?&city=new-york,los-angeles
And I am trying to change the url using .htaccess to:
http://www.site.com/country/tours/dynamic-part/new-york,los-angeles
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} (^|&)city=([^&]*)(&|$)
RewriteRule ^country\/tours\/([a-zA-Z0-9]*)\/.+city=([^\/]*)$ http://www.site.com/country/tours/$1/$2 [L,R=301]
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Any ideas? I though I was close but not anymore :/
The RewriteRule does NOT match the query string, see
http://httpd.apache.org/docs/current/mod/mod_rewrite.html#what_is_matched
So the .+city part of the rule will never match.
This should work tho...
RewriteCond %{QUERY_STRING} (^|&)city=([^&]*)(&|$)
RewriteRule ^country\/tours\/([a-zA-Z0-9]*)\/ http://www.site.com/country/tours/$1/%2 [L,R=301]
The subsitution can read back-referenecs to the RewriteCond pattern.