Have read lots of posts on this, but still can't figure it out.
I have an old page that used lots of query parameters. I want to redirect traffic to this page to my homepage. A normal 301 redirect doesn't work when parameters are present.
I want:
/page.php
/page.php?a=1
/page.php?a=2
all to redirect to example.com/.
This code was already in my htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
I added the following, immediatly below:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/page\.php$
RewriteCond %{QUERY_STRING} a=(.*)$
RewriteRule ^(.*)$ https://www.example.com/? [R=301,L]
Redirect 301 /page.php https://www.example.com/
What's happening is that only the last line is triggering. If I navigate to example.com/page.php it redirects as expected.
But if I navigate to example.com/page.php?a=1 then it redirects to example.com/?a=1 and gives me a 404 error.
Does anyone have the solution? Thanks
No need to capture query string because you want to redirect all traffic to page.php including page.php?query so , just just match against this page URI :
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/page\.php$
RewriteRule ^(.*)$ https://www.example.com/? [R=301,L]
Note: clear browser cache then test
Related
I am trying to do 301 redirects from a few pages to a single URL using .htaccess.
I would like the redirection to work regardless of whether the called page has an extension or not.
But the redirection only works correctly if the page is called with the extension.
If there is no extension, then the page is normally loaded in the browser.
So when I call "mywebsite.com/previous-page-2" the page is displayed well, while I would like it to be automatically redirected to "mywebsite.com/new-page.html"
here is my current code :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [OR,NC]
RewriteCond %{https} off
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [R=301,L]
RedirectMatch 301 "^(previous-page-1)(.html)?$" "new-page.html"
RedirectMatch 301 "^(previous-page-2)(.html)?$" "new-page.html"
</IfModule>
Please note:
There is also a piece of code added by wordpress to have nice urls (which by the way allows to display the page without extension):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I can open my site like this:
www.mysite.com
or like this:
www.mysite.com/index.php
I want to create a htaccess rule that redirects www.mysite.com/index.php to www.mysite.com. But all my attempts have other side effects. I've tried:
Redirect index.php home.php
RewriteRule ^index.php?$ home.php [NC,L]
RewriteRule ^index.php/?$ redirect_to_home.php [NC,L]
But all of these mess up the original index.php call. So it does redirect but then the normal mysite.com link doesnt work anymore.
Any ideas?
Could you please try following.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} index\.php
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$
RewriteRule ^ %1 [R=301,L]
Explanation:
Making RewriteEngine On to make the rules work.
Mentioning condition by RewriteCond to REQUEST_FILENAME which checks if mentioned file in browser is present.
Then checking if THE_REQUEST which has complete details of request(including URL and GET/POST method) if it has index.php in it.
Now checking if REQUEST_URI is having index\.php in requested url, where saving everything before it to temp buffer memory to retrive its value later(basically its domain name).
Finally in RewriteRule to redirect complete URL with index.php to till domain name only as per requirement(R=301 is for permanent redirection on browser side).
Use this redirect rule to remove /index.php from any path:
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]
I have a Wordpress set up which requires redirection when the user enters the root of the site to a static HTML file start.html
http://www.myhomepage.com/
Redirect to
http://www.myhomepage.com/start.html
Wordpress adds url rewrites for calls to index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I only want to redirect
http://www.myhomepage.com/
and not
http://www.myhomepage.com/buy/
This will not work as all requests to Wordpress goes through index.php.
redirect /index.php /start.html
I guess I need a redirect for all pure requests to index.php and not those with query strings. Though I can not figure out how to rewrite it.
The reason is that I want all users that enters the site to get a static html of the wordpress site. Only when the user starts to navigate the site should request be made against wordpress.
EDIT: I required the rule to apply only on GET requests
It can be done without mod_rewrite, with mod_dir and the DirectoryIndex Directive.
DirectoryIndex start.html
Add this line:
RewriteRule ^/?$ /start.html [L]
just after this line:
RewriteBase /
In addition to #Oussama solution, as I have a form which posts to itself on the first page. Posting to start.html would not work. I added a condition so that rule only applies to GET requests. That way form post would be sent to index.php as usual.
RewriteCond %{REQUEST_METHOD} GET
RewriteRule ^/?$ /start.html [L]
Final file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_METHOD} GET
RewriteRule ^/?$ /start.html [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I am new to htaccess and even though I get the part to have a custom URL, I can't manage to figure out https redirect combined with having a custom URL. All the options I have found online have given me a redirect loop.
So, suppose I have a page called: mysite.com/hello.php and another one called mysite.com/bye.php.
I want mysite.com/hello.php to become mysite.com/one-hello and bye.php to become mysite.com/two-bye. This is how I have it in my htaccess now:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^one-hello$ hello.php
RewriteRule ^two-bye$ bye.php
But now I want hello.php to always show up as https but bye.php to always show up as http. How could I write all of these?
Thanks!
Have your rules like this:
RewriteRule ^one-hello$ hello.php [L]
RewriteRule ^two-bye$ bye.php [L]
RewriteCond %{HTTPS} off
RewriteRule ^hello\.php$ https://%{HTTP_HOST}%{REQUEST_URI} [L,NC,R]
RewriteCond %{HTTPS} on
RewriteRule ^bye\.php$ http://%{HTTP_HOST}%{REQUEST_URI} [L,NC,R]
hello I have a website with a main domain , and subdomains.. running over apache web server and centOS... I also have a url rewrite mechanism such as
http://subdomain.domain.com/buy-a-new-car
Id like to redirect all subdomain requests to main domain, keeping the url rewrite like this:
http://domain.com/buy-a-new-car
the .htaccess code i have so far results in this :
http://domain.com/index.php?buy-a-new-car
id like to get rid of the ( index.php? ) part, but I am new to writing .htaccess directives and confused by REGEX
here is my current code :
RewriteEngine on
RewriteBase /
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1
RewriteCond %{HTTP_HOST} ^subdomain.domain.com$
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
any help would be greatly appreciated !
You need your redirects to happen first, then at the end, do your routing to index.php:
RewriteEngine on
RewriteBase /
Options -Indexes -Multiviews
# redirect subdomains to main domain
RewriteCond %{HTTP_HOST} ^subdomain.domain.com$
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
# redirect direct accesses to index.php
RewriteCond %{THE_REQUEST} \ /index\.php\?/([^&\ ]+)&?([^\ ]*)
RewriteRule ^ /%1?%2 [L,R=301]
# route everything to index.php internally
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1
Why not use Redirect and/or RedirectMatch and save the Rewrite stuff for the real rewriting of URLs? See http://httpd.apache.org/docs/current/rewrite/avoid.html
I find this a much more "self documenting" approach which doesn't force you to understand all the rewrite logic each time you go in there.