RedirectRule with .htaccess - regex

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/

Related

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

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/

how to sepratly on admin folder in htaccess

Friends i am php developer i did customize the url in example is
From : http://example.net/page.php?post_id=contact/
To : http://example.net/contact/
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#RewriteBase use only home/phtml/www/ this kind of path
RewriteRule ^([a-zA-Z-/]+)$ page.php?post_id=$1 [QSA]
RewriteRule ^([a-zA-Z-/]+)/$ page.php?post_id=$1 [QSA]
</IfModule>
its working fine on above that htaccess code but it have some problem. What is i got "admin" folder for control panel purpose. In which how to redirect to http://example.net/admin/ correctly any option this in htaccess?
My error is: http://example.net/page.php?post_id=admin/
How to Solve this?
You can skip all directories from your rewrite rule. Use:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z/-]+)/?$ page.php?post_id=$1 [L,QSA]
</IfModule>
Also you should keep unescaped hyphen at first or last place in a character class.
I combined 2 of your rules into one using optional trailing slash.

htaccess - redirect to exact folder with different rules

I have an htaccess that makes pretty url. The content is like:
RewriteEngine On
RewriteBase /
RewriteRule ^(en|ru)/([a-z0-9_\-]+)/?$ index.php?lang=$1&section=$2 [QSA,L]
but I also have a folder - /ru/forum and I need my htaccess to have an exception for this url to parse /ru/forum/index.php . How can I do it?
Thanks in advance
You can have it like this:
RewriteEngine On
RewriteBase /
RewriteRule ^(en|ru)/((?!forum/)[a-z0-9_-]+)/?$ index.php?lang=$1&section=$2 [QSA,L]

Redirect all www to non-www including subdomains

I have two separate WordPress sites on the same domain, one installed in the following way:
http://subdomain1.domain.tld and http://subdomain2.domain.tld.
http://domain.tld redirects to subdomain1.
I need to remove all www. to non-www (this now works as tested on http://htaccess.madewithlove.be/), www.domain.com now returns domain.com but i am still getting a 404 page returned when i type in a subdomain prepended with www.
My current code, with the suggestions by #icabod now looks like this.
# 301 redirect www to non-www url
# including subdomains
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule (.*) http://%1/$1 [R]
</IfModule>
mod_rewrite is working with other redirections, I do not want to use a PHP redirection. I have no access to my registrar's CNames so I cannot redirect them through there. I have placed a .htaccess file in which I have the below code in the root folder and have tried placing it in the subdomains root folder as well.
This is driving me bananas !
Thank you.
Don't forget that you can use a part of the RewriteCond expression in the rule, just like you can match a part of the RewriteRule expression:
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule (.*) http://%1/$1 [R]
The %1 matches the part in braces in the RewriteCond, and the $1 matches the part in the RewriteRule. This rewrite does make the assumption that you are only using http on the standard port 80, but will basically remove any www. from the beginning of the requested URI. From that point, redirecting domain.tld to subdomain1.domain.tld should be straightforward:
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://subdomain1.example.com/$1 [R]
As an aside, you probably shouldn't be using [R=301] until you have the rewrite working correctly, as the 301 means it's a permanent rewrite, but during testing it is prone to change.

htaccess RedirectMatch regular expression for URL?

On my Wordpress blog, I used to have a plugin that I no longer need. The plugin used to create a bunch of URLs that looked like this:
http://tambnguyen.com/manage-subscriptions?srp=532&sra=s
with the postID being 532. How do I redirect the query strings so that the above URL will redirected to:
http://tambnguyen.com/?p=532
I've tried a few method without any luck (there's an optional "/" after "manage-subscriptions"
<IfModule mod_rewrite.c>
RedirectMatch 301 ^/manage-subscriptions/?\?srp=(\d{1,5})(.*)$ http://tambnguyen.com/\?p=$1
</IfModule>
Please help. Thanks!
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 /
RewriteCond %{QUERY_STRING} ^srp=([^&]+) [NC]
RewriteRule ^manage-subscriptions/?$ /?p=%1 [L,R=301,NC]