I have a URL www.example.com/page/abcderf
=> abcderf <= is dynamic, it is never the same value.
I would like to redirect with .htacess from :
www.example.com/page/abcderf
to
www.example.com/page/
How to do ?
My attempt:
RewriteCond %{REQUEST_URI} (\page\.[a-zA-Z0-9])$
RewriteRule ^(.+)/$ http://www.example.com/page/ [R=301,L]
You don't need any conditions, one single line should work:
RewriteRule ^page/(.*) /page/ [R=301,NC,L]
or as MrWhite mentioned if you don't need to know what was in the URL
RewriteRule ^page/. /page/ [R=301,NC,L]
NC = no case
L = Last (don't run anything after)
R=301 = 301 redirect
Related
redirect url exact match with htaccess not redirect if query string or anything after index.php
Redirect This Page: https://example.com/demo/index.php
To Page: https://example.com/ (home page)
But Do Not redirect: https://example.com/demo/index.php/*
Do NOT redirect: https://example.com/demo/index.php/password
Do NOT redirect https://example.com/demo/index.php?m=page
not redirect if any other combination
only redirect https://example.com/demo/index.php to https://example.com/
this script not working
RewriteEngine On
RewriteBase /
RewriteRule ^demo/index.php /demo/index.php?m=page[L,NC,END]
RewriteRule ^demo/index.php$ https://example.com/ [L,R=301]
only redirect https://example.com/demo/index.php to https://example.com/
If you only want to redirect that exact URL, without a query string then you need to reverse your existing rules and include an additional condition that checks that the QUERY_STRING is empty. The RewriteRule only matches against the URL-path, which excludes the query string.
You are also missing a space before the flags argument in the first rule.
For example:
# Redirect exact URL to home page
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^demo/index\.php$ / [R=302,L]
# Redirect other URLs with query string OR path-info
RewriteRule ^demo/index\.php /demo/index.php?m=page [NC,L]
You don't need L and END.
Test with 302, and change to 301 only once you have confirmed it works OK. To avoid caching issues.
You may use these rules in site root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+demo/index\.php\s [NC]
RewriteRule ^ / [L,R=301]
RewriteRule ^demo/index\.php$ $0?m=page [QSA,NC,L]
This was created by htaccess redirect generator I use very often.
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^demo/index\.php$ https://example.com/? [R=301,L]
Maybe "?" sign is redundant because of empty query string match.
Not sure, but You also can try to add
RewriteCond %{REQUEST_METHOD} !=POST
in case You can't log in. All together:
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^demo/index\.php$ https://example.com/? [R=301,L]
I would like to redirect the urls which have query strings to the top page like this
from
https://cococo.com?abc=123
to
https://cococo.com
because when i type https://cococo.com?abc=123 it also display the top page so I think it's not seo-friendly.
how to whrite the .htaccess to solve this problem?
the follow one is wrong.
RewriteCond %{QUERY_STRING} .+
RewriteRule ^\?(.*)$ %{ENV:REWRITEBASE}?$1 [L,R=301]
You can use this rule to remove query string only on landing page:
RewriteEngine On
RewriteCond %{QUERY_STRING} .
RewriteRule ^/?$ /? [L,R=301]
I'm having trouble setting up the following 301 redirects.
I'm trying to redirect:
https://www.example.com/blog/wp-includes/js/jquery/jquery.js?ver=1.11.3
to the home page:
https://www.example.com/
This is what I tried but doesn't work.
Redirect 301 "/blog/wp-includes/js/jquery/jquery.js\?ver\=1\.11\.3" "/"
As stated on this page https://simonecarletti.com/blog/2009/01/apache-query-string-redirects/
Unfortunately, neither Redirect nor RedirectMatch allow you to specify a query string for the redirect source.
So you have to switch to using mod_rewrite, i.e. use this instead:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/blog/wp-includes/js/jquery/jquery\.js$
RewriteCond %{QUERY_STRING} ^ver=1\.11\.3$
RewriteRule ^(.*)$ / [R=301,L]
QueryString is not part of match in Redirect directive, you need to match against %{THE_REQUEST} using mod-rewrite,try :
RewriteEngine on
RewriteCond %{THE_REQUEST} /blog/wp-includes/js/jquery/jquery\.js\?ver=1\.11\.3 [NC]
RewriteRule ^ /? [L,R]
You can remove the ? from the target path if you want to forword the query strings to homepage.
I'm trying to redirect an old URL :
www.mydomain.com/prodotti/G/YToyOntzOjg6IklEX01BUkNBIjtzOjI6IjI3IjtzOjEwOiJJRF9NT0RFTExPIjtzOjM6IjU2OCI7fQ..
to this one , shorter and SEO friendly:
www.mydomain.com/moto/aprilia/caponord_1200
I'm doing this writing a 301 redirect on htaccess, this is the line that I use:
Redirect 301 /prodotti/G/YToyOntzOjg6IklEX01BUkNBIjtzOjI6IjI3IjtzOjEwOiJJRF9NT0RFTExPIjtzOjM6IjU2OCI7fQ.. http://www.mywebsite.com/moto/aprilia/caponord_1200
But when i insert on the browser the old URL the redirect result is :
www.mywebsite.com/moto/aprilia/caponord_1200?/prodotti/G/YToyOntzOjg6IklEX01BUkNBIjtzOjI6IjI3IjtzOjEwOiJJRF9NT0RFTExPIjtzOjM6IjU2OCI7fQ..
What's is wrong in my 301 redirect? how can I obtain a pure redirect without URLs merging?
Thanks
this is the htaccess with a modrewrite test
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|media|js|robots\.txt)
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteRule ^/moto/aprilia/capondor_1200$ /prodrotti/G/YToyOntzOjg6IklEX01BUkNBIjtzOjI6IjI3IjtzOjEwOiJJRF9NT0RFTExPIjtzOjM6IjU2OCI7fQ.. [R=301, L]
Your htaccess should look like this
RewriteEngine on
RewriteRule ^prodrotti/G/YToyOntzOjg6IklEX01BUkNBIjtzOjI6IjI3IjtzOjEwOiJJRF9NT0RFTExPIjtzOjM6IjU2OCI7fQ\.\.$ /moto/aprilia/capondor_1200? [R=301,L]
RewriteCond $1 !^(index\.php|images|css|media|js|robots\.txt)
RewriteRule ^(.*)$ /index.php?/$1 [L]
Try a rewrite rule like this
RewriteEngine On
RewriteRule ^/moto/aprilia/capondor_1200$ /prodrotti/G/YToyOntzOjg6IklEX01BUkNBIjtzOjI6IjI3IjtzOjEwOiJJRF9NT0RFTExPIjtzOjM6IjU2OCI7fQ [R=301]
You may need to replace YToyOntzOjg6IklEX01BUkNBIjtzOjI6IjI3IjtzOjEwOiJJRF9NT0RFTExPIjtzOjM6IjU2OCI7fQ in the rewrite rule the whole string, as I assume .. means it's longer than what you provided?
Currently I set a rewrite rule like so, to produce clean and simple url's.
.htaccess
RewriteRule ^/about$ about.php [L]
But what i need to do is something a little different, the other way around. For example if a user clicks the following link
about
They would go to the about page /about
Currently the about page resides at index.php?a=about&b=user and this can't be changed unfortunately. As you can see it does not look very nice in the browser address bar.
EDITED
I am using a pre-made script from phpdolphin, which is working fine. But the url are all index.php based and i would like to clean them up
Currently the only code within the .htaccess file
RewriteEngine on
RewriteCond %{request_filename} -f
RewriteRule ^(.*) $1 [L]
RewriteRule ^(([^/]*)+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L]
Add this rule before your existing rule:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?a=([^&]+)&b=user[\s&] [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteRule ^([^/.]+)/?$ index.php?a=$1&b=user [L,NC,QSA]
You can add this RewriteRule to redirect the request when user hit index.php?a=about&b=user
RewriteCond %{QUERY_STRING} ^(.*)a=about(.*)$ [NC]
RewriteRule ^index.php /about [L,NC,R=301]
or you can use php header() function in index.php to redirect the request:
if ($_REQUEST['a'] == about) {
header('Location: /about');
exit;
}
Try this i checked and it works ...
RewriteEngine On
RewriteRule ^([A-Za-z0-9-+_%*?]+)/([A-Za-z0-9-+_%*?]+)/?$ index.php?a=$1&q=$2 [L]
Note: add additional signs between [] if necessary
OR This
RewriteRule ^([^~]+)/([^~]+)/?$ index.php?a=$1&q=$2
I like to use this code because it's short and matches everything except for ~ which is very very rare to see in a url