htaccess redirect all liks but specific ones - .htaccess

how can I do this in htaccess
I have
/main_dir/([a-z-]+)/dir1 - this should remain as it is
/main_dir/([a-z-]+)/dir2 - this should remain as it is
/main_dir/([a-z-]+)/dir2/UQ13A -this should remain as it is
but
/main_dir/([a-z-]+)/UQ13A -should take me to /main_dir/$1/dir2/UQ13A
UQ13A - can be any string containing [a-zA-Z0-9-]
I am not so familiar with negative lookahead, lookbehind
for now I have something like
/main_dir/([a-z-]+)/(?!(dir1|dir2)) /main_dir/$1/dir2/$2
which is not working

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
RewriteRule ^main_dir/([a-z-]+)/(?!(?:dir1|dir2)/)(.+)$ /main_dir/$1/dir2/$2 [L,NC]

Related

How to rewrite the base URL?

I need to make a rewrite rule that if anyone writes website url (the main one),
www.domain.com
it will show
www.domain.com/1
But if someone looks for anything else, for example:
www.domain.com/2
it will show the requested url,
www.domain.com/2
I need only to rewrite the base root /.
Thank you for your help,
Adam :)
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 ^/?$ /1 [L]

Htaccess - rewriting subfolder names as GET parameters

Can somebody please tell me the exact code to convert #1 to #2? I'm new to .htaccess and I am having trouble with this.
http://www.domain.com/folder/subfolder1/subfolder2/subfolder3/
http://www.domain.com/folder.php?param1=subfolder1&param2=subfolder2&param3=subfolder3
I want the URL in the address bar to keep being #1 so that is what the user sees. Meanwhile, the server treats the URL as page #2 so I can use GET variables in my code.
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 ^(folder)/(subfolder1)/subfolder2/subfolder3/?$ /$1.php?param1=$2&param2=$3&param3=$4 [L,QSA,NC]

Change URL but stay on the same page using htaccess

I have a URL:
www.example.com/property-listings/united-states/colorado/denver/denver-co-office-space
I want to stay on the same page above but simply display the URL in the address bar like this:
www.example.com/property-listings/united-states/colorado/denver/office-space
How do I accomplish this using htaccess and rewrite rules?
If I understood right, try writing a rule like this one:
RewriteEngine on
RewriteRule property-listings/united-states/colorado/denver/office-space http://www.example.com/property-listings/united-states/colorado/denver/denver-co-office-space [L]
OK. You didn't supply a pattern or mentioned there was any, so I have to guess the pattern is up to /denver/ subdirectory. Try this:
RewriteEngine on
RewriteRule ^(property-listings/united-states/colorado/denver/)(office-space)/?$ $1denver-co-$2 [L]
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 ^(property-listings/united-states/colorado)/(denver)/(office-space)/?$ $1/$2/$2-co-$3 [L,NC]

.HTACCESS Rewriting Issue

Can any check given code of .htaccess file and let me know what's wrong with it and why it's not working?
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^itinerary/([a-zA-Z0-9_-]+)/([0-9]+)\$ itinerary-details.php?tId=$2
I want to rewrite url as below:
www.domain.com/itinerary-details.php?tId=2&tName=agra-delhi-tour
to
www.domain.com/itinerary/2/agra-delhi-tour
and
www.domain.com/itinerary-details.php?page=1&tId=2&tName=agra-delhi-tour
to
www.domain.com/itinerary/2/agra-delhi-tour/1
Please help me in doing this.
Thanks
Seems that you have the two sections in your regex reversed. This should work for both your cases:
^itinerary/([0-9]+)/([a-zA-Z0-9_-]+)/?(.*) itinerary-details.php?tId=$1&tName=$2&page=$3
I also just noticed that you were escaping the $ which would also cause problems.
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 ^itinerary/([^/]+)/([^/]+)/?$ /itinerary-details.php?tId=$1&tName=$2 [L,QSA,NC]
RewriteRule ^itinerary/([^/]+)/([^/]+)/([^/]+)/?$ /itinerary-details.php?page=$3&tId=$1&tName=$2 [L,QSA,NC]

Remove folder with HTACCESS

When users go to the following url:
http://mysite.com/folder/subfolder?a=blah&b=blah
I need to point them to content that is in the root folder while keeping the variables intact:
http://mysite.com/?a=blah&b=blah
Since http://mysite.com is a virtual host, this solution should also leave other base URLs alone.
I'd prefer not to do a full redirect because we use log tracking in our system and we need to have the headers return a 200 code for the logs (maybe this doesn't matter).
I know that this is a fairly straightforward question for someone who really understands .htaccess redirects. Thanks in advance!
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 %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^folder/subfolder/?$ / [L,NC]

Resources