I am simply trying to rewrite and redirect automatically this:
From mysite.com/sub/index.php?channel=rai1
To mysite.com/sub/channel-rai1.html
I've tried with this:
RewriteRule ^sub/channel-([^/]*)\.html$ /sub/index.php?channel=$1 [L]
but the problem is that the redirection does not happen. Why?
Changing your rewrite rule to:
RewriteRule ^sub/channel-([^/]*)\.html$ /sub/index.php?channel=$1 [R=301]
will work. You were writing the rule, but you weren't telling it to change it in the browser.
[L] tell it to stop applying any rules that follow. [R=301] will do the actual redirection of the url to the new one and tell browers/search engines that the new url is the permanent one to use. You can use [R=301, L] which will combine the two.
EDIT:
I read the question wrong, my apologies
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /sub/index\.php\?channel=(.*)&data=(.*)\ HTTP
RewriteRule ^sub/index.php /sub/channel-%2-%3.html? [R,L]
#Internal rewrite
RewriteRule ^sub/channel-([^/]*)-(.*)\.html$ /sub/index.php?channel=$1&data=$2 [L]
The above should change http://example.com/sub/index.php?channel=NBC&data=2012/02/12 to http://example.com/sub/channel-NBC-2012/02/12.html
Related
I already have a couple of rules set up such as
RewriteCond %{HTTP_HOST} ^www.pipcanadaltd\.com$ [NC]
RewriteRule .?$ http://ca.pipglobal.com%{REQUEST_URI} [R=301,L]
And I also have rewrites for a few PHP pages such as:
RewriteRule ^products/eye-protection-experts/$ prod-expert-eyewear.php [NC,L]
For some reason, when I went to create a simpler 301 redirect, it is not working. Here is what I have:
RewriteRule ^products/construction-channel-experts/$ ^products/construction-safety-solutions/ [R=301]
I'm really confused on why this doesn't work.
You can use this rule for 301 redirect:
RewriteRule ^products/construction-channel-experts/?$ /products/construction-safety-solutions/ [R=301,L,NC]
Note that ^ is used for matching start position in regex and it can only be used in pattern which is on left hand side.
You should keep redirect rules before internal rewrite rules.
How can i remove a file name in url with mod_rewrite.
ex: searchpage-some+search.html in to some+search.html
This is my .htaccess code code.
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /searchpage\.php\?search=(.*)\ HTTP
RewriteRule ^searchpage\.php$ /searchpage-search-%2.html? [R,L]
#Internal rewrite
RewriteRule searchpage-search-(.*)\.html$ searchpage.php?search=$1 [L]
This is for a search form that uses $_GET request. This works well only thing is that i want to remove the file name. I would really appreciated if anyone can help out.
In your example you have searchpage-some+search.html but in your htaccess you are actually rewriting it to searchpage-search-some+search.html.
But to remove the searchpage-search from links, just remove searchpage-search- from the RewriteRule. Your htaccess would be something like
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /searchpage\.php\?search=(.*)\ HTTP
RewriteRule ^searchpage\.php$ /%2.html? [R,L]
#Internal rewrite
RewriteRule (.*)\.html$ searchpage.php?search=$1 [L]
I've wrote .htaccess file to convert urls to SEO friendly :
the original url is :
http://palestinianz.com/?page=person&p=Alex-Atalla
the content of .htaccess is :
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /?page=person&p=$1 [L]
it should produce a link like this :
http://palestinianz.com/Alex-Atalla.html
But it makes no effect although I put the file in the root of my website !
where is the problem ?
If you want the URL to change in the address bar, you need to redirect the browser, then rewrite on the server. The first part of this answer explains the process that you want: https://stackoverflow.com/a/11711948/851273
So looking at the second list, the process is to redirect the browser if an ugly link is request to a nice looking link:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?page=person&p=([^\ ]+)
RewriteRule ^$ /%1.html? [R=301,L]
Now, on the server end you need to internall rewrite it back to the ugly url, which is more or less what you already have:
RewriteRule ^([^/]*)\.html$ /?page=person&p=$1 [L]
You can try:
RewriteCond %{REQUEST_URI} (.+)\.html$
RewriteRule ^(.+)\.html$ ./index.php?page=person&p=$1 [L]
Only problem with this is any .html page will go through this. May want to change to the following:
# URL: domain.com/person/Alex-Atalla.html
RewriteCond %{REQUEST_URI} (.+)/(.+)\.html$
RewriteRule ^(.+)/(.+)\.html$ ./index.php?page=$1&p=$2 [L]
This will allow you to have more robustness in changing the the page variable as well
i have this rule in .htaccess file which is internal redirect can anybody tell me how to make this external redirect using R301
#Internal Rewrite#
RewriteRule ^(.*)page/([^/\.]+)/?$ $1?page=$2 [L]
I want to make it something like below [but it is not working something is wrong here..]
RewriteCond %{THE_REQUEST} \?page=([0-9]+).*
RewriteRule (.*) $1/page/%1? [R=301,L]
RewriteRule (.*)/page/1 $1 [R=301,L]
Any expert here can help me..Thank you in avance.
Add R=301 flag like this:
RewriteRule ^(.*)page/([^/.]+)/?$ $1?page=$2 [L,R=301,QSA,NC]
Ok I have this Rewrite Rule in my .htaccess:
RewriteRule ^settings https://%{HTTP_HOST}/user_settings.php
This redirects "http://domain.com/settings" to "https://domain.com/user_settings.php".
How do I make it so that it redirects "http://domain.com/settings" to "https://domain.com/settings"? Meaning, how can I redirect a url from HTTP to HTTPS while still keeping the Redirect Short url in .htcaccess?
Thanks!
EDIT: Updating answer after question was clarified.
You can do something like this:
RewriteCond %{HTTPS} !=on
RewriteRule ^settings(/?)$ https://%{SERVER_NAME}%{REQUEST_URI} [R,NC]
RewriteCond %{HTTPS} =on
RewriteRule ^settings(/?)$ /user_settings.php [L,NC]
The first RewriteCond will check if HTTPS is off and the following RewriteRule will only happen if it is.
The first RewriteRule will match either of the following:
http://domain.com/settings
http://domain.com/settings/
The (/?) allows the trailing forward slash to be optional. The R in [R,NC] forced this to be a redirect, the NC means it's not case-sensitive.
The second RewriteCond will again check HTTPS, but this time to make sure it's on.
If HTTPS is on, and you're at your directory (meaning, the first rewrite was successful), it will then do a rewrite to user_settings.php. The L in [L,NC] means that if this rule is matched, it's the last RewriteRule it should follow.