I have pages that are called via a query strung from a cgi.
xyz.cgi?page=abc.html
The problem that I have is that I get bot requests for pages that do exist, but the request has characters after the .html and the result is a 404.
Is there a way in .htaccess to strip off everything after the .html extension?
You can use the following rule to remove chars after the html extension :
RewriteRule ^(.+\.html).+$ /$1 [L,R]
This will redirect
/foo.htmlchars
to
/foo.html
Or
RedirectMatch ^/(.+\.html).+$ /$1
EDIT :
To redirect /xyz.cgi?page=foo.htmlchars to /xyz.cgi?page=foo.html you may using the following :
RewriteEngine on
RewriteCond %{THE_REQUEST} /xyz\.cgi\?page=(.+\.html).+\sHTTP [NC]
RewriteRule ^ /xyz.cgi?page=%1 [L,R]
Related
I want to redirect below two dynamic url from old url's to new url's using .htaccess and i have done this application using codeigniter.
1) This are the dynamic url's and there are more than thousands of url's in my database.
Old(From) URL
https://www.example.com/area-name/pangothe-263001
New(To) URL
https://www.example.com/pangothe-263001
2) This are the amp version of the above url.
Old(From) URL
https://www.example.com/amp-area-name/pangothe-263001
New(To) URL
https://www.example.com/pangothe-263001/amp
I have tried with below code
RewriteRule ^area-name$ http://www.example.com/area-name [R=301,L]
RewriteRule ^amp-area-name$ http://www.example.com/amp-area-name [R=301,L]
But not redirecting to the new urls. How can do this redirect using .htaccess.
Keep these 2 redirect rules just below RewriteEngine line:
RewriteEngine On
RewriteRule ^area-name/(.+)$ /$1 [R=301,L,NC,NE]
RewriteRule ^amp-area-name/(.+)$ /$1/amp [R=301,L,NC,NE]
# remaining rules go below this
Try This
RewriteRule ^area-name/pangothe-263001/?$ $1/pangothe-263001$2 [R=301,L]
Try with below rules,
Canonical links Redirect
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/area-name/(.*)$
AMP links Redirect
RewriteRule ^ https://www.example.com/%1 [R=301]
RewriteCond %{REQUEST_URI} ^/amp-area-name/(.*)$
RewriteRule ^ https://www.example.com/%1/amp [R=301]
This rule translates URLs like
http://www.example.com/index.php?page=about to friendly URLs like http://www.example.com/about
RewriteRule ^([a-zA-Z_]+)?$ index.php?page=$1 [NC,L]
It's working well. But, currently old URLs (like http://www.example.com/index.php?page=about) are indexed in google. So, in addition to that rule I need to redirect all old URLs (like http://www.example.com/index.php?page=about) to new friendly URLs (like http://www,example.com/about) so when visitor old URL he gets redirected to friendly URL. I wrote that rule:
RewriteCond %{QUERY_STRING} ^page=([^&]+)$ [NC]
RewriteRule ^index\.php$ http://www.example.pl/%1? [NC,R=301,L]
and it redirects to new URL but instead of showing a website content, it throws an error ERR_TOO_MANY_REDIRECTS.
Any help?
You are getting too many redirect error because of your internal rewriteRules,as they internaly forward the request to the same location after first rewrite iteration, to fix this , add the following at top of your htaccess,
RewriteEngine on
RewriteCond %{ENV_REDIRECT_STATUS} 200
RewriteRule ^ - [L]
or you can match against %{THE_REQUEST} instead of %{QUERY_STRING} to avoid rewrite loop/too many redirect error
RewriteCond %{THE_REQUEST} /index\.php\?page=([^\s]+) [NC]
I'm trying to learn how to use htaccess rewrite, but I just can't get the hang of it. What I'm trying to do is make the following urls:
mysite.com/
mysite.com/views/clients
mysite.com/views/projects
mysite.com/views/estimates
...look like this:
mysite.com/
mysite.com/clients
mysite.com/projects
mysite.com/estimates
It should be simple enough. But I just can't make it work.
Assuming the URLs in your app are already of the form /clients, not /views/clients, then try the following in the .htaccess file in your document root:
RewriteEngine On
# Remove "views" from all (typed) URLs with external redirect
RewriteCond %{THE_REQUEST} /views/([^\ ]*)
RewriteRule .* /%1 [R,L]
# Internally rewrite specific requests back to /views
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(clients|projects|estimates)/(.*) /views/$1/$2 [L]
Once you are satisfied this is working then change the R (temporary) redirect to R=301 (permanent).
This checks against THE_REQUEST before the redirect in order to prevent a redirect loop.
My site was hacked not too long ago and a lot of URLs were created in the following format:
http://example.com/prematurely.asp?skin=pspfsffdproblems=nq....
is there anyway I can re-direct all these wildcard URLs based on prematurely.asp to a single page? for example:
http://example.com/newpage
.htaccess file is as follows:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^nowcosmetic\.co.uk$ [NC]
RewriteRule ^(.*)$ http://nowcosmetic.co.uk/$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/questions-and-answers\.html$
RewriteRule .* http://nowcosmetic.co.uk/botox.html [R=301,L]
RewriteRule ^prematurely\.asp /botox? [R=301,L]'
You can do this by adding a rewrite rule
RewriteEngine on
RewriteRule ^prematurely/(.*) /newpage [R=301,L]
As mentioned in comments, if these URLs are the result of a hacked site which has now been resolved then you are better off (SEO wise) serving a 404 (or 410 Gone) instead of redirecting.
However, if you still want to redirect...
re-direct all these wildcard URLs based on prematurely.asp to a single page
At the top of your .htaccess file (in your document root):
RewriteEngine on
RewriteRule ^prematurely\.asp /newpage? [R=301,L]
Note that RewriteEngine On should only appear once in your file (at the top).
This simply matches against "prematurely.asp" (ignoring the query string - as per your request). The trailing ? on the RewriteRule substitution strips the original query string from the rewritten URL.
I'm trying to use mod_rewrite to redirect an existing file to a different URL. I'm using the following and it has no effect. I've tried several variations of which don't work.
RewriteEngine on
AddHandler x-httpd-php .php3
# AddHandler x-httpd-php5 .php .php4
# This file exists, but this redirect doesn't work
RewriteRule ^show.php?id=review-1$ /review/1/super-baseball-2020/ [R=301,L]
Does it by chance have something to do with the url params?
You cannot have query string in RewriteRule. RewriteRule matches only URL part that's why your new rule is not working. You need to have a separate RewriteCond to match QUERY string. It should be re-written like this:
# for external redirect from /shows.php?id=review-1 to /review/1/super-baseball-2020/
RewriteCond %{THE_REQUEST} ^GET\s/shows\.php\?
RewriteCond %{QUERY_STRING} (?:^|&)id=([^-]*)-(.*)(?:&|$) [NC]
RewriteRule ^ /%1/%2/super-baseball-2020/? [R=301,L,NC]
# for internal redirect from /review/1/super-baseball-2020/ to /shows.php?id=review-1
RewriteRule ^([a-z0-9]+)/([0-9]+)/([a-z0-9-]+)?/?$ shows.php?id=$1-$2 [L,NC,QSA]