combine RewriteRule htaccess 301 Redirect - regex

I'm using the below htaccess rewrite rules to redirect old url files to the new ones.
The first RewriteRule redirects to new url file if the file path requested is fares/ or airfares/ or anyword-fares/ etc.
The second RewriteRule redirects to new URL file only if the url path requested is flights/.
My 1st question is, did I write them correctly or leave anything out?
2nd question is, can I combine the two RewriteRules into one or use a rewrite condition to combine them into one?
I'm asking because I will be adding more that will redirect to the new url file of airfares-flights/
# For security reasons, Option followsymlinks cannot be overridden.
#Options +FollowSymLinks
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteRule ^.*fares/(.*)$ /airfares-flights/$1 [R=301,NC,L]
RewriteRule ^flights/(.*)$ /airfares-flights/$1 [R=301,NC,L]

Yes your rules are correct.
Yes both rules can be combined into one.
Use this .htaccess:
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteRule ^(?:.*fares|flights)/(.*)$ /airfares-flights/$1 [R=301,NC,L]

Related

how do I solve a 404 error from a pretty url?

I'm trying to redirect my content removing the .php extension from all the files using this .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
and when I try to include any other redirect like this one give me a 404 error:
RewriteRule ^create/(.*)$ ./create.php?app=$1
I'm trying to use something like https://myurl.com/create/37744e17-98ff-58a9-8996-7cf746e508b9 instead of https://myurl.com/create.php?id=37744e17-98ff-58a9-8996-7cf746e508b9, but looks like the start of my .htaccess it's giving the main issue, there's a way to solve this?
You've not stated where you are putting that rule. It would need to go first. The order is important.
Try it like this instead:
# Disable MultiViews
Options -MultiViews
RewriteEngine On
RewriteRule ^create/([a-f0-9-]+)$ create.php?id=$1 [L]
# General extensionless ".php" URLs
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule (.*) $1.php [L]
Note that you used an id URL parameter in your example, but used app in your rule? I've changed this to id.
Based on your example URL, the id URL parameter can consist of the characters a-z, 0-9 and hyphens only.
The RewriteBase directive is not required. The ./ prefix on the substitution string is not required and should be removed.
In your original rule that appends the file extension there is no need to check that the request does not map to a directory before checking that it does map to a file (with a .php extension). You were also potentially checking a different file-path to the one being rewritten to.
Note that you need to ensure that MultiViews is disabled, otherwise create.php is called but without the URL parameter.

.htaccess to pass parameters and remove the page name

I have the following folder structure in my website:
example.com/
reports/
causelist/
final_causelist.php
I have a link / URL to the final_causelist.php page is as:
http://example.com/reports/causelist/final_causelist.php?wb_sno=23&dated=2020-10-26&j_names=Mr.+ABC
I want to access the page like the following:
http://example.com/23/2020-10-26/Mr.+ABC
For the above, I created .htaccess in the causelist folder with the following code:
RewriteEngine on
RewriteBase /reports/causelist/
Options -Indexes
RewriteRule ^(\d+)/([a-z-]+)/(.+)/$ /final_causelist.php?wb_sno=$1&dated=$2&j_names=$3 [L,B]
But it does not show the page and complaint that the requesting page is not found on this server?
Your regex is not matching date part correctly and you must remove leading / from target as you're using a RewriteBase.
Options -Indexes
RewriteEngine on
RewriteBase /reports/causelist/
RewriteRule ^(\d+)/([\d-]+)/(.+?)/?$ final_causelist.php?wb_sno=$1&dated=$2&j_names=$3 [L,QSA]
Could you please try following.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(\d+)/(\d{4}-\d{2}-\d{2})/(.+?)/?$
RewriteRule ^.*$ /reports/causelist/final_causelist.php?wb_sno=%1&dated=%2&j_names=%3 [QSA,NE,NC,L]

How Can I 301 Redirect All Old URLs

I just updated my old shopping cart to a new one and I would like to know if some genius out there can give me an htaccess REWRITE rule that will redirect all of my old links to my new sites homepage.
Mainly, I want any link that begins with mySite.com/catalog to be redirected to the homepage. Please help!!!!
Option 1:
You can use this rule as first rule in your DocumentRoot/.htacess:
RewriteEngine On
RewriteRule ^catalog(/|$) /? [L,NC,R=301]
Option 2:
You can use this rule as first rule in your /catalog/.htacess:
RewriteEngine On
RewriteBase /catalog/
RewriteRule ^ /? [L,NC,R=301]
This should be as simple as placing the following in your Apache config or in .htaccess of web root directory.
RewriteEngine On
RewriteRule ^/?catalog/? / [L,R=301]

Regex htaccess rewrite to strip url prefix

So many questions come close to it, but not quite there, and I'm afraid regex makes my head spin!
After a site recode, which slightly amended some urls - which we don't want to go 404 in Google, I am looking for a 301 redirect to remap:
http://www.oursite.com/listing-product-name-1234.html
to
http://www.oursite.com/product-name-1234.html
...you can see we just want to drop "listing-". The "1234" is a unique ID per product, and the product name can be multiple words (hyphen separated) by the way.
Thanks very much for the help regex heads!
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^listing-(.+?\.html)$ /$1 [L,R=301,NC]
Try this:
RewriteCond %{REQUEST_URI} ^/listing-(\w+-)+\d+\.html
RewriteRule ^listing-(.*) $1

301 redirection help on wordpress

I'm searching for a 301 redirect rules in .htaccess for the last one week. I have visited many similar pages here too. But all are not working for me. So I'm asking help from masters like you.
I'm using wordpress blogging platform. Before I was using Joomla and that time so many 404 errors was there on my website. There are more than 10,000s of 404 errors. Mainly two category of 404 errors. Examples are example.com//component/option,com_xmap/Itemid,44/component/xxx/yyy/menu1/menu2/something/ and example.com/index.php/component/option,com_xmap/Itemid,44/component/xxx/yyy/directory/menu1/menu2/something.html. Like these too many urls. I think this happened due to some cache problem in Joomla and these urls were created one by one on the homepage of a module. After I disabled cache for that module the problem was solved, but google indexed all these bad urls already.
I think it's better to do a 301 redirect for those two categories of 404 error urls to the homepage or to www.freezonal.com/sitemap.xml since these urls are only indexed by google and are not seen in search results.
Currently I'm using this on my .htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteCond %{QUERY_STRING} .*
RewriteRule ^index.php/(.*)$ /? [R=301,L]
Is there any 301 redirection code for redirecting those two categories of 404 urls to the main website and is there any other suggestion for better SEO. I'll be very thankful to you, if you solve this.
First, WordPress' rewrite rules must be on the bottom, because they slurp up any 404s. Anything after them isn't going to work correctly. So add new rules above WordPress' rules.
RewriteEngine On
RewriteBase /
RewriteRule (index.php/|/)?component/option http://example.com/ [L,R=301]
Something like that should work. I don't know if you want to keep any of your /component/ URLs working, so you may need to remove the question mark from the RewriteRule.
In order for the rewrite to work, make sure that you wrap your rewrite conditions in the mod_rewrite.c tags. Then turn turn on RewriteEngine and set your RewriteBase. I don't think the redirects work outside of the mod_rewrite tags.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} .*
RewriteRule ^index.php/(.*)$ /? [R=301,L]
</IfModule>
If you only want to only redirect these two paths to the homepage, try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^component* / [R=301,L]
RewriteRule ^index.php/component* / [R=301,L]
</IfModule>
Try that out and see if it works.
Also, be sure to check out the documentation on apache.org here http://httpd.apache.org/docs/current/mod/mod_rewrite.html