Redirect 404 to search page - .htaccess

Becouse in google webmasters I have 550.000 not found pages - 404 - I want to make some redirections to the search page.
To be userfoul for the users, need to go to the search page, but is realy complicated for me, becouse I need to EXTRACT the search phrase from the 404 url:
Here are the 2 type of url with 404 errors, and need to be redirected.
DOMAIN/movie/12298295-Iron-Man-3
DOMAIN/serie/15108-The-Walking-Dead/seasons/3/episodes/10
Of corse, I need to extract JUST THE NAME of the movie, or the serie and redirect to this url
DOMAIN/search?q=Iron-Man-3
DOMAIN/search?q=The-Walking-Dead
Well this is the URL by parts:
For "movies" just have 4 parts:
DOMAIN - of corse, is the domain name of the website
/movie/ - is created based on the content type (movie or serie)
12298295- is the ID for this item and can be form - 1 - to
infinite
Iron-Man-3 - the name of the movie (is wath I want to get)
For "series" - THE SAME like for the movies, but at the end can have more parts, and the full URL can be: (all this url, for an normal page with NO 404, are ok)
DOMAIN/serie/15108-The-Walking-Dead/seasons/3/episodes/10 - Link to episode page
DOMAIN/serie/15108-The-Walking-Dead/seasons/3/ - Link to some season page
DOMAIN/serie/15108-The-Walking-Dead/ - Link to the TOP page for this serie
Please note, I need to redirect this url, JUST if the result is an 404 page (ErrorDocument 404), becouse the same type of url are used in all pages.
Here is my actual .htacces
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php[^/] /$1? [L,R=301,NC,NE]
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php(?:/(.*))?$ /$1$2? [L,R=301,NC,NE]
RewriteCond %{QUERY_STRING} ^q= [NC]
RewriteRule ^busqueda\b /busquedas [R=301,L,QSA]
# Redirect Trailing Slashes...
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Many thanks for your help

The following shall work for both movies and series:
RewriteRule ^(?:movie|serie)/\d+-([^/]+) /search?q=$1 [R=301,L,NC]

Related

.htaccess Pages open even though the URL is broken

I have a problem with my website opening pages that should show 404 error. It seems that only the first part of the URL is taken into account.
Here's an example
example.com/page.php - (opens - correct)
example.com/asdasd.php - (error 404 - correct)
example.com/asdasd.php/asdasd - (error 404 - correct)
example.com/page.php/asdasd - (opens - incorrect)
example.com/page/ - (opens - incorrect)
example.com/page/asdasd (opens - incorrect)
I would like to get an Error 404 and if I feel that the URL is important also be able to Redirect it. Now I can't even Redirect URLs that for example look like this "example.com/page.php/asdasd"
The closest answer that I found was this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=302,L]
RewriteCond %{THE_REQUEST} ^(?:GET|HEAD)\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+?)/?$ $1.php [L]
But the problem was that I got something like this:
Input URL
example.com/page.php/asdasd
Output URL
example.com/page
I need it for the .php extention to stay. And if possible instead of "Redirecting" to the example.com/page.php, I'd like to get an Error 404
This is what I currently have in the htaccess file:
ErrorDocument 404 /404.php
ErrorDocument 401 /404.php
ErrorDocument 403 /404.php
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*) http://www.example.com/$1 [L,R=301]
RewriteRule ^index\.php$ / [R=301,L]
Sorry if this is a duplicate but I don't even know what I should look for.
example.com/page.php/asdasd - (opens - incorrect)
This is due to a feature called Path Info - if Apache manages to match the first part of the requested URL to an existing file, then it will serve that file, and pass the rest of the path along via environment variables (so that a script could in theory make use of this information.)
The AcceptPathInfo directive makes it possible to disable this behavior.
https://httpd.apache.org/docs/2.4/mod/core.html#acceptpathinfo

hteaccess: Pretty url by htaccess showing 404 error

I am beginner in codding. I want make url pretty. I search this question on google then i found i have to use .htaccess. I tried htaccess code.
Here is my URL looklike
website.com/project-details.php?Residential/The-Serenas/1
I want to make url like website.com/Residential/The-Serenas/1
I want to remove entire "project-details.php?" word from url by htaccess.
here is my .htaccess code
Options +FollowSymLinks
RewriteEngine On
RewriteBase /basename/
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /project-details\.php\?([^\s&]+) [NC]
RewriteRule ^ view/%1? [R=302,L]
RewriteCond %{THE_REQUEST} /project-details\?([^\s&]+) [NC]
RewriteRule ^ view/%1? [R=302,L]
# internally rewrites /view/ABC123 to project-details.php?ABC123
RewriteRule ^view/([A-Za-z0-9_#./#&+-]+)$ project-details.php?$1 [L,NC,QSA]
# PHP hiding rule
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
Above htaccess code work and url replacce to website.com/view/Residential/The-Serenas/1
If i remove view from this line RewriteRule ^ view/%1? [R=302,L].
Then page goes to 404 error. If i dont remove view then code worked but view show on url.
I dont want to show anything on url only remove "project-details.php?" this.
This rule is to check the request uri whether is /project-details.php and any query string follow behind, and redirect user to the uri that matches the query string. %1 is the backreference and provide access to the grouped parts (in parentheses) of the pattern, in this case ([^\s&]+). For example, if uri /project-details.php?Residential/The-Serenas/1 is requested, the user will be redirected to /Residential/The-Serenas/1.
RewriteCond %{THE_REQUEST} /project-details\.php\?([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
This rule is to check if user requests existing file and directory, if not, then the uri is rewritten internally (only server knows the destination uri, user does not know this). For example, if uri /Residential/The-Serenas/1 is requested, the server will rewrite the uri to project-details.php?/Residential/The-Serenas/1.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9_#./#&+-]+)$ project-details.php?$1 [L,NC,QSA]

Implementing "friendly" URLs using .htaccess

I tried some of the other answers I could find in here, but it didn't work out. It's really simple though.
I want
/page?id=PAGENAME
to be accessible AND redirected to
/PAGENAME
Can you help me?
EDIT:
It feels like my already messed-up .htaccess file needs to be included in here. I already have basic rewriting enabled, but this feature is needed for two other "special pages". In the requested solution above, I would therefore just replace "page" with the two pagenames (it's danish names, so I thought it was easier this way).
Currently I have this. If you have any improvements to it, it's appreciated - but I just want this to work with the requested solution aswell.
# Options -Multiviews -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
# Always on https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
# remove trailing slash
#RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
#301 Redirect everything .php to non php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+\.)+php?\ HTTP
RewriteRule (.+)\.php?$ http://MYURL.dk/$1 [R=301,L]
#Hide the .php from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
#301 Redirect everything mistype after file extension -
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
#301 Redirect everything to current url -
RedirectMatch permanent /(.*).php/.* http://MYURL.dk/$1.php
RewriteCond %{REQUEST_FILENAME} -D
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L]
#301 Redirect from non www to www
RewriteCond %{HTTP_HOST} ^www.MYURL.dk [NC]
RewriteRule (.*) http://MYURL.dk/$1 [R=301,L]
#301 redirect index.php to /
RewriteBase /
RewriteCond %{REQUEST_URI} index.php
RewriteRule .* http://MYURL.dk/ [R=301,L]
#Deny access to songs
RewriteCond $1 !(loadmedia)\.php
RewriteRule ^songs/(.*)$ - [L,F]
Generally the URL in address bar should be like
www.siteurl.com/pagename/ for seo purpose and then read this url from .htaccess using rule which gives this query string parameter values in your php file.
.htaccess rule can be like
RewriteRule ^(.*)/$ /page?id=$1 [QSA,L]
It looks like you are wanting to implement "friendly" (or "pretty") URLs, making the URLs more friendly for you users (search engines don't really mind what your URLs look like).
The first step is to change all your on-page links to use the new "friendly" URL. So, you links should all be of the form /pagename (not /page?id=PAGENAME).
Then, in .htaccess, you need to internally rewrite this "friendly" URL into the real URL that your server understands. This can be done using mod_rewrite. In the .htaccess file in your document root:
# Enable the rewrite engine
Options +FollowSymLinks
RewriteEngine On
# Rewrite the "friendly" URL back to the real URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} !^id=
RewriteRule ^([\w-]*) /page?id=$1 [L]
If the file does not exist (!-f) and does not contain the id URL param then internally rewrite the request from /<pagename> to /page?id=<pagename>. This assumes your <pagename> consists only of the characters a-z, A-Z, 0-9, _ and -.
If this is a new site and the old URLs are not already indexed or referenced by external sites then you can stop here.
However, if you are changing an existing URL structure then you also need to externally redirect the real (ugly) URL to the "friendly" URL before the above internal rewrite. (This is actually what you are asking in your question.) In order to prevent a rewrite loop we can check against %{THE_REQUEST} (which does not change when the URL is rewritten).
# Redirect real URLs to "friendly" URLs
RewriteCond %{THE_REQUEST} \?id=([\w-]*)
RewriteRule ^page$ /%1? [R=302,L]
Change the 302 (temporary) to 301 (permanent) when you are sure this is working OK. Permanent redirects are cached by the browser so can make testing a problem.
So, in summary, with the above two parts shown together:
# Enable the rewrite engine
Options +FollowSymLinks
RewriteEngine On
# Redirect real URLs to "friendly" URLs
RewriteCond %{THE_REQUEST} \?id=([\w-]*)
RewriteRule ^page$ /%1? [R=302,L]
# Rewrite the "friendly" URL back to the real URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} !^id=
RewriteRule ([\w-]*) /page?id=$1 [L]
The order of directives is important. External redirects should nearly always come before internal rewrites.
UPDATE#1:
I want /concept?id=NAME to go to /NAME and /studio?id=NAME to go to /NAME - there's 5-10 different "pages" from both concept and studio. [Corrected according to later comment]
Since id=NAME maps to /NAME you can achieve all 10-20 redirects with just a single rule:
RewriteCond %{QUERY_STRING} ^id=(NAME|foo|bar|baz|abc|def|ghi)
RewriteRule ^(concept|studio)$ /%1? [R,L]
This will redirect a URL such as /studio?id=foo to /foo.
As with all external redirects this should be one of the first rules in your .htaccess file.
Change R to R=301 when you have tested that it is working OK.
To make this more "dynamic", ie. match any "NAME" then change the CondPattern, for example:
RewriteCond %{QUERY_STRING} ^id=([\w-]*)
UPDATE#2:
If the path part of the URL (ie. concept or studio) is required then you can modify the RewriteRule substitution like so:
RewriteCond %{QUERY_STRING} ^id=([\w-]*)
RewriteRule ^(concept|studio)$ /$1/%1? [R,L]
Which will redirect /concept?id=foo to /concept/foo.
Or, to be completely "dynamic" (bearing in mind this will now capture anything):
RewriteCond %{QUERY_STRING} ^id=([\w-]*)
RewriteRule ^([\w-]+)$ /$1/%1? [R,L]

Add get variables to url throung htaccess file

I want to add one get parameter to the every url on my wprdpress website except the index page.
For example,
Consider website name to be www.example.com
If www.example.com is hit than nothing should happen
But if I hit www.example.com/events than a get parameter viz. "home" be appended to it. Final URL should be as follows:
www.example.com/events/?home
I want to use this parameter for further processing.
my current .htaccess file contains following
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{QUERY_STRING} !lp-variation-id
RewriteRule ^go/([^/]*)? /wp-content/plugins/landing-pages/modules/module.redirect-ab-testing.php?permalink_name=$1 [QSA,L]
RewriteRule ^landing-page=([^/]*)? /wp-content/plugins/landing-pages/modules/module.redirect-ab-testing.php?permalink_name=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
and i m working on wordpress project.
Try this:
RewriteCond !^/$
RewriteRule ^/(.*)$ /$1?home
Depending on your hosting environment the URL may or may not start with a slash, so you might have to remove it from the RewriteRule.

301 redirect from query string to simple URL

I've had a good look through the first ten pages of search results for "301 redirects" and can't find the answer so here goes...
I've moved from a crappy old CMS that didn't give my pages nice URLs to one that does and I want to set up a 301 redirect for my key pages.
Current URL: http://www.domain.com/?pid=22
New URL: http://www.domain.com/contact/
I'm using Wordpress and my current htaccess file looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Any help would be awesome!
Give this a try. All you need to do is check to see if you are on page X and then redirect to page Y. Consider RewriteCond statements to be 'if' statements. If you need more redirects just duplicate the last two lines and edit the paths.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.domain\.com\/?pid=22$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/contact [L,R=301]
You have to check the query string for the value in the "pid" variable and then redirect if the value in that variable matches a page you want to redirect. You can do this with the "RewriteCond" and "RewriteRule" directives like this:
RewriteEngine On
RewriteBase /
# Redirect pid=22 to http://www.domain.com/contact
RewriteCond %{QUERY_STRING} ^pid=22$
RewriteRule ^(.*)$ http://www.domain.com/contact [R=301,L]
You can repeat the "RewriteCond" and "RewriteRule" directives to create additional redirects.

Resources