I am trying to redirect every request of my site like this:
http://www.example.com/four_digit_number
to
http://www.example.com/index.php?m=four_digit_number
I already have a redirection rule like this:
RewriteEngine On
RewriteCond %{HTTP_REFERER} ^$ [OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?sipem.com
RewriteRule ^([^\/]+\.jpg)$ /images/managers/$1 [L]
which in redirects visitors from
www.example.com/four_digit_number.jpg
to
www.example.com/images/managers/four_digit_number.jpg
I need to add this rule to that.
You can use a new rule for this:
RewriteEngine On
RewriteRule ^(\d{4})/?$ /?m=$1 [L,QSA]
RewriteCond %{HTTP_REFERER} ^$ [OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?sipem.com
RewriteRule ^([^/]+\.jpg)$ /images/managers/$1 [L]
Related
I am trying to achieve a clean url that looks like http://localhost/slugs/this-is-my-first-slug-post
but instead I have http://localhost/slugs/news.php?newsheadline=this-is-my-first-slug-post
I have an htaccess file with this code but it doesn't seem to work.
RewriteEngine On
RewriteRule ^[A-Za-z-]+/([A-Za-z0-9-]+)/?$ news.php?newsheadline=$1 [NC,L]
You can use this rule:
RewriteEngine On
RewriteBase /slugs/
RewriteCond %{THE_REQUEST} /news\.php\?newsheadline=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ news.php?newsheadline=$1 [QSA,L]
I am new to this.
Code from my .htaccess file goes like this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]
Redirect 301 /abc/ /abcnew/
I want this to redirect from www to non-www i.e., from http://www.example.com to http://example.com
I copied:
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]
this code from here Generic htaccess redirect www to non-www.
I also checked in /etc/apache2/mods-enabled folder on my linux server. There "rewrite.load" this module is present.(I think this might mean that rewrite is enabled on my server, but correct me if I am wrong.)
Redirect 301 /abc/ /abcnew/
and just FYI this above code works fine(its redirecting my old links to new links).
I also tried this.
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
Doesn't work for me.
Please help. Thanks in advance...
Edit:
this link I found this. But not sure what should be edited. Can anyone please point out.?
You need to place external (full) redirect rules before internal rewrite ones and also make sure to use mod_rewrite rules only.
Try this:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]
RewriteRule ^abc/?$ /abcnew/ [L,NC,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
My .htaccess in a folder looks like:
RewriteEngine On
RewriteBase /profile/
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?username=$1
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/profile/$1 [R=301,L]
Basically, if you go to www.mySite.com/profile/Username, my index.php file takes 'Username' as a $_GET variable, and the URL will look clean (www.mySite.com/profile/Username)
However if you go to mySite.com/profile/username (omitting the www), the URL will look like http://www.mySite.com/profile/index.php?username=username
How can I make it so only the www is added without messing the URL up?
Thanks
Ordering of rules does matter in .htaccess.
Try this code instead:
RewriteEngine On
RewriteBase /profile/
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/profile/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?username=$1 [L,QSA]
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]
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.