Is there a way to match /#user in Apache rewrite rules? - regex

I am trying to redirect users from /#adam to /user/adam on my Litespeed server which uses .htaccess files.
I have already tried the following :
RewriteEngine On
RewriteBase /
RewriteRule ^\/#.+$ /user/$1
I expected it to redirect, but it does not. Even tried some variations like
RewriteEngine On
RewriteBase /
RewriteRule ^\/#(.+)$ /user/$1

You are close!
Do this:
RewriteBase /
RewriteRule ^#(.+)$ /user/$1
Notice I removed the leading \/
If you need to rigorously test htaccess rules then I highly recommend this gem: https://htaccess.madewithlove.be/

Related

RedirectRule with .htaccess

I am new to the .htaccess file.
I want to make pretty URLs but the server always gives me 404 or 500 errors.
For example, I want to redirect
http://www.example.com/dir1
to
http://www.example.com/dir1/file1.html
without showing file1.html in the address bar.
I've tried
RedirectRule /dir1/$ /dir1/file1.html but the server says 404.
The .htaccess is in root.
What should I do?
Remember that .htaccess is per directory directive and Apache strips the current directory path (thus leading slash) from RewriteRule URI pattern.
You can use this rule in root .htaccess:
RewriteEngine On
RewriteBase /
RedirectRule ^dir1/?$ dir1/file1.html [L,NC]
OR use this rule in /dir1/.htaccess:
RewriteEngine On
RewriteBase /dir1/
RedirectRule ^/?$ file1.html [L]
This htaccess rule will do the trick.
RewriteEngine On
RewriteBase /
RedirectRule ^dir1/?$ dir1/file1.html [L]
For the record the terminating /? means that you to redirect links with /dir1 and links with /dir1/

Remove parts of url with htaccess

I have the following url:
http://www.trashtheweb.com/index.php/home/archive/?p=1
I would like to rewrite this into http://www.trashtheweb.com/archive/?p=1
I tried the following, but this is giving me 404 errors.
RewriteEngine on
RewriteRule ^index.php/home/(.*)$ $1
RewriteEngine on
RewriteBase /
RewriteRule ^index\.php/home/(.*)$ $1 [L,R=301,QSA]
This code do the job (tried on a blank configuration). If not, you probably have some other files interfering.

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

HTACCESS : RegEx match replace for URL

I want the htaccess Redirect 301 to do the following using regex:
http://example.com/folder/abc_123_123.htm
to
http://example.com/abc-123-123.shtml
The 3 objectives of new URL are
delete /folder/
replace all _ with -
replace htm with shtml
Best I'm aware, your point #2 cannot be done without a RewriteMap, which requires some pesky configuration, or multiple rules.
This implementation should be generic and work for any number of underscores, but it's expensive in that it might trigger many redirect (in fact, potentially enough to fire redirect errors in browsers if you've tons of underscores in your paths):
RewriteBase /
RewriteRule ^(folder/[^_]*)_(.*\.html?)$ /$1-$2 [L,R=301]
RewriteRule ^folder/(*+)\.html?$ /$1.shtml [L,R=301]
Alternatives include enumerating use-cases as needed, or (better, I suspect) rewriting the request to a perl or php script and doing the regexp_replace + redirect 301 from there.
RewriteBase /
RewriteRule ^folder/(*+)\.html?$ /folder/redirect.php [L,QSA]
RewriteEngine On
RewriteBase /
RewriteRule ^folder/([a-z]+)_(\d+)_(\d+)\.htm$ http://example.com/$1-$2-$3.shtml [L,R=301]

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