How to redirect using .htaccess? - .htaccess

I need my site to redirect from this url:
mysite.com/old/dir/param1/param2/...
to
mysite.com/dir/param1/param2/...
I need to keep the params, just remove the "old" in the url.
How can I add this rule to my .htaccess?
Thanks!!!
Here's my .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
RewriteRule ^old/dir/(.*) mysite.com/dir/$1 [R=301,L]
</IfModule>

The right rule for it was
and I had to place it before all the other rules. So my .htaccess looks like:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^old/dir/(.*) /dir/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
</IfModule>
Thanks anyway! :)

You could just write it like this.
Rediret 301 mysite.com/old/dir/param1/param2/(.*) mysite.com/dir/param1/param2/$1
If you're planning to redirect the entire directory of old/dir to dir/, then try this:
Redirect 301 mysite.com/old/dir/(.*) mysite.com/dir/$1
The next option you could try is, Url Rewriting.
Assuming that you have set your Rewrite Engine on and have set the Rewrite Base, add these lines:
RewriteRule ^old/dir/(.*) mysite.com/dir/$1 [R=301,L]

Related

Force HTTPS in htaccess while also only making public folder accessible

This .htaccess file is in my /public_html/ folder:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
And this .htaccess is inside my /public_html/public/ folder
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
I need to add a rule like the following:
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
I get an issue with it redirecting too many times.
I need to still use the public folder as my root and the front controller inside /public/ but I want to direct all traffic to https.
Can someone help me understand how to go about this?
Do I need to change my rewrite rules pertaining to the /public/ folder?
Add http->https rule in .htaccess of /public_html/public/ folder as this one:
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteRule (.*) https://%{HTTP_HOST}/$0 [R=301,NE,L]
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
Make sure to test in a new browser or completely clear browser cache.

Is there a way to setup a dynamic htaccess rewrite

I'm need some help with an htaccess rewrite. I would like to do a clean url setup that is dynamic.
For example
mydomain.com/pagename/var1/var2/
mydomain.com/pagename2/var1/var2/var3/etc
mydomain.com/pagename3/var1/var2/var3/etc
I would like the first level to be alway be the filename in the rewrite?
mydomain.com/pagename.php?v1=var1&v2=var2&v3=var3
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$0\.php -f [NC]
RewriteRule ^([^.]+)/?$ $0.php?v1=$1&v2=$2&v3=$v3 [L]
This will do your job done,
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^home/{0,1}$ pagename.php?v1=1[QSA,L]
RewriteRule ^sone/{0,1}$ pagename.php?v1=2[QSA,L]
RewriteRule ^music/{0,1}$ pagename.php?v1=3[QSA,L]
RewriteRule ^video/{0,1}$ pagename.php?v1=3[QSA,L]
</IfModule>
and the link look like that: mydomain.com/home but the real link is mydomain.com/pagename.php?v1=1

ReWrite Rule to a URL Friendly Page and User Profile

I want to use two RewriteRules to redirect Pages and Users to the right link but I'm coundn't achieve what I want, maybe someone could help me...
I want to redirect any Url Friendly page from:
http://www.example.com/my-url-friendly-page
to
http://www.example.com/index.php?cod=my-url-friendly-page
AND User Profiles from:
http://www.example.com/profile/username
to
http://www.example.com/profile/index.php?user=username
This is my htaccess, the first rule is working ok but I couldn't fix the second one...
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?cod=$1
RewriteRule ^/profile/(.*)$ index.php?user=$1 [QSA,L]
</IfModule>
Can anyone help me with this ?
Reorder your rules like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^profile/(.*)$ index.php?user=$1 [QSA,L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?cod=$1 [L,QSA]
</IfModule>

htaccess rewrite help needed

I was trying to write a rewrite rule, but so far couldn't achieve what I want.
my site is in a subdirectory and the htaccess file is in that directory.
What I want is, if I go to http://example.com/site/ or http://example.com/site/index.phpit should rewrite to
http://example.com/site/index.php?key=2
The value of key won't change. It is fixed. I want it to load by default but hide from the url.
What I tried is,
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /site/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site/index.php?key=2&$1 [L,QSA]
</IfModule>
it's not working.
but if I go to address http://example.com/site/anything then it works.
Can you please help me in this?
I don't understand htaccess very well.
Thanks in advance.
Have it this way:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /site/
RewriteRule ^(index\.php)?$ index.php?key=2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?key=2&$1 [L,QSA]
</IfModule>

htaccess 301 redirect example.com/beta/xyz to example.com/xyz

I need a general rule that maps every URL in the /beta subdirectory to the corresponding URL in the root; essentially I need to remove /beta from all URLs.
In case it makes any difference, the URLs are dynamically generated by WordPress.
Currently my .htaccess file is:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
#END WordPress
Can you tell me where to put the new lines?
Thank you!
Could you try this?
RewriteEngine On
RewriteRule ^beta/(.*)$ http://example.com/$1 [R=301,L]
Your question is not entirely clear, but I would bet that what you want is having a Wordpress installed in somedir/beta appear in yoursite.com/ instead of yoursite.com/beta/. The rules you pasted are in somedir/beta/.htaccess, and are the default Wordpress rules. You must leave those alone.
What you need for that is to put the following rules in the root directory, as in, somedir/.htaccess, after changing example.com to your own domain. Your webserver first reads this root .htaccess, and when it does, it will know to rewrite requests to /beta.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/beta/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /beta/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ beta/index.php [L]
</IfModule>
More info in the Codex:
https://codex.wordpress.org/Giving_WordPress_Its_Own_Directory

Resources