How do I force a 404 error through htaccess for URLs that contain "mobile=no"?
This question is not the same as a previously answered question. This one deals with returning a 404 for URLs that contain a certain parameter. In my case, "mobile=no".
RewriteCond %{QUERY_STRING} ^mobile=no$
RewriteRule ^(.*)$ 404.html [R=404,L,NC]
Try this :
RewriteEngine on
RewriteCond %{THE_REQUEST} \?mobile=no [NC]
RewriteRule ^ - [R=404,L]
Related
I need to 301 redirect an old url that contained a get parameter in the url.
I need to 301 the URL:
http://www.website.com/choose?cat=womens
to this URL:
http://www.website.com/womens
I have searched and tried without it working:
RewriteCond %{QUERY_STRING} cat=womens
RewriteRule ^choose\.php$ /womens [L,R=301]
Where am I going wrong?
You're almost correct, just 2 issues:
.php wasn't there in your original URI after choose as per the question
You need to add ? in target to strip original query string
You can use:
RewriteCond %{QUERY_STRING} (?:^|&)cat=([^&]+) [NC]
RewriteRule ^choose(?:\.php)?$ /%1? [L,R=301,NC]
Try these:
RewriteCond %{QUERY_STRING} ^cat=womens$
RewriteRule ^choose$ http://www.website.com/womens? [R=301,L]
So I need to redirect a mass of old Wordpress short urls that begin with question marks such as:
/?post=731
Or
/?cat=73
I need them to be instead /page
I tried
RewriteRule /?cat=73$ http://www.mydomain.com/page? [R=301,L]
Didn't work
I tried
RewriteCond %{QUERY_STRING} ^cat=73$
RewriteRule ^/$ page? [L,R=301]
Didn't work.
Each old URL will have to be manually redirected to a new url. Meaning:
/?cat=73 goes to /category-73
/?cat=2 goes to /main-category
/?post=731 goes to /this-page
These are just examples.
Each old link will have a NEW link - no one size fits all.
I have about 500 URLs like this. Anyone got any good ideas?
Try this rule:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^cat=73$ [NC]
RewriteRule ^ /category-73? [L,R=301]
RewriteCond %{QUERY_STRING} ^cat=2$ [NC]
RewriteRule ^ /main-category? [L,R=301]
I'm trying to set 301 redirects on pages that have 'page=1' in the URL to stop duplicate content issues.
e.g.
http://www.domain.com/reviews/?page=1
to
http://www.domain.com/reviews/
I've tried all of the variations I can find and can't seem to get anything to work.
RewriteRule ^reviews(/)?page=1$ http://www.domain.com/reviews/ [L,R=301]
RewriteRule ^(.*)/?page=1 http://www.domain.com/reviews/ [R=301,L]
RewriteCond %{QUERY_STRING} page=1
RewriteRule ^reviews$ http://www.domain.com/reviews/ [R=301,L,NE]
None of these have worked. I'm not really sure what else to try.
There are multiple different sections of the site that I need to do this for:
reviews
news
videos
accessories
hardware
An overall solution to redirect all ?page=1 URLs to their relevant section would be best.
Use this code to redirect every URI with ?page=1 to one without the query parameter:
RewriteCond %{QUERY_STRING} ^page=1(&|$) [NC]
RewriteRule ^ %{REQUEST_URI}? [R=301,L]
Or else if you want to redirect ONLY /reviews URI then
RewriteCond %{QUERY_STRING} ^page=1(&|$) [NC]
RewriteRule ^(reviews)/?$ /$1? [R=301,L]
the question is already answered, i just would like to mention that your rules are not working because you didn't append a trailing ? to the new url in the rewrite rule
I'm trying to rewrite a URL so that it's user/search engine friendly, then 301 redirect the original URL to the new one.
At the moment the posts generate like this:
example.com/blog/post.php?s=nice-post
But I'd like them to look like this:
example.com/blog/nice-post
Here's what I've got so far:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{QUERY_STRING} ^s=(.+)$
RewriteRule post.php /%1? [R=301,L]
RewriteCond %{REQUEST_URI} !post.php
RewriteRule ^([a-zA-Z0-9_-]+)$ post.php?s=$1
Unfortunately, this clashes with the 404 redirect, sending all pages not found to the blank post.php file, and I can't work out why. Is there a better way of approaching the URL rewrite/redirect?
Thanks for having a look :)
Elaborating on tdammers' suggestion about header()
in your post.php, before outputting anything, decide if it's a valid post or not (for example by looking its id up in the database).
if( !$valid ) {
header( 'HTTP/1.0 404 Not Found' );
echo "404";
exit;
}
i.e. if the page was found invalid, PHP sends out a 404 header to the browser to notify it that this is a missing page. Unfortunately, there's no straightforward way to redirect to Apache's default 404 page, so you'll have to output something yourself -- but since you already have a custom 404 page, you may just include() that.
Going to assume you've got these rules in an htaccess file in your /blog/ directory. Try adding a RewriteBase and removing the leading slash from your query string backreference:
RewriteEngine On
RewriteBase /blog/
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{QUERY_STRING} ^s=(.+)$
RewriteRule post.php %1? [R=301,L]
RewriteCond %{REQUEST_URI} !post.php
RewriteRule ^([a-zA-Z0-9_-]+)$ post.php?s=$1
I've recently refactored an existing CodeIgniter application to use url segments instead of query strings, and I'm using a rewriterule in htaccess to rewrite stuff to index.php:
RewriteRule ^(.*)$ /index.php/$1 [L]
My problem right now is that a lot of this website's pages are indexed by google with a link to index.php. Since I made the change to use url segments instead, I don't care about these google results anymore and I want to send a 404 (no need to use 301 Move permanently, there have been enough changes, it'll just have to recrawl everything).
To get to the point: How do I redirect requests to /index.php?whatever to a 404 page? I was thinking of rewriting to a non-existent file that would cause apache to send a 404. Would this be an acceptable solution? How would the rewriterule for that look like?
edit:
Currently, existing google results will just cause the following error:
An Error Was Encountered
The URI you submitted has disallowed
characters.
I tried something like:
RewriteRule ^index\.php?(.*)$ /no-exist.html [R=404,L]
But it caused an internal server error.
edit2:
CodeIgniter is already sending '400' errors, will this be sufficient to get my pages removed from google?
RewriteRule's R[=code] flag allows code only from range 300-400.
Don't use redirect R flag - just try to rewrite to an unexciting page:
UPDATED:
Two redirects are apposed to each other - use RewriteConds to escape the interference.
Complete .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/index.php.*
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(.*)$ /no-exist.html [L]
RewriteCond %{REQUEST_URI} !^/index.php.*
RewriteCond %{REQUEST_URI} !^/no-exist.html.*
RewriteRule ^(.*)$ /index.php/$1 [L]
Note: /no-exist.html actualy doesn't exist. Suppose, it will help you.
There is a special HTTP status code 410 GONE to tell the World to remove resource:
The requested resource
/index.php
is no longer available on this server and there is no forwarding address. Please remove all references to this resource.
To send this code use [G|gone] flag in rewrite rule:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/index.php.*
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(.*)$ / [G,L]
RewriteCond %{REQUEST_URI} !^/index.php.*
RewriteRule ^(.*)$ /index.php/$1 [L]