.htaccess map specific url to remove sub directory and index.html - .htaccess

This will be a common question however I cannot get this to work at all and have been pissing around with it for hours now.
What I need to do is have this url domain.com/landing/subdirectory/index.html appear as domain.com/subdirectory.
This will only be used for the one file so I don't need anything fancy, everything I have tried just doesnt seem to work. I'm so used to it being easy peasy to do within a framework like yii with proper routing set up but cant seem to figure it out in raw .htaccess >.<

Try adding this to the htaccess file in your document root:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+landing/subdirectory/(index\.html)?($|\ |\?)
RewriteRule ^ /subdirectory [L,R=301]
RewriteRule ^subdirectoy/?$ /landing/subdirectory/index.html [L]

Related

htaccess is working but does not replace the url

I'm trying to modify the subdomain name in the URL to make it look nicer. My current URL look something like:
www.mystore.com/productInfo.php?cPath=11_11&productID=222
So, I want to make it nicer by Rewrite in .htaccess in main with this code:
RewriteEngine On
RewriteRule ^productInfo/([0-9_-]+)/([0-9_-]+) productInfo.php?cPath=$1&productID=$2 [NC,L]
When I run and test it on the URL by typing www.mystore.com/productInfo/11_11/222 in the URL it works well. However, when this page is redirected by a different page or is 'refreshed' with a self redirecting a href= link(in which the link is written in php by the previous programmer), the above old link is still visible in the URL instead of the new one.
I am still a beginner while I suspect that I might need to change something in the cPanel/Apache(I think) for this but currently, I am still do not have access to the cPanel control. Is there anything that I might have missed to write in the .htaccess file or I really do need the cPanel control or any other reasons?
Any help is appreciated. Sorry that I could not find similar questions on this.
You can use the following :
RewriteEngine On
RewriteBase /
#redirect /productInfo.php?cPath=foo&productID=bar to /productInfo/foo/bar
RewriteCond %{THE_REQUEST} /productInfo\.php\?cPath=([0-9_-]+)&productID=([0-9_-]) [NC]
RewriteRule ^ productInfo/%1/%2? [L,R=301]
#rewrite new URL to the old one
RewriteRule ^productInfo/([0-9_-]+)/([0-9_-]+) productInfo.php?cPath=$1&productID=$2 [NC,L]

.htaccess redirect whole website structure but leave some of the old structure intact

I have searched but can't quite find the answer to this exact situation:
My website structure is as follows
www.example.co.uk/old-folder1/old-folder2/old-page-name
I needed to redirect the whole structure to:
www.example.co.uk/new-folder1/old-folder2/old-page-name
I successfully did this with:
RewriteRule ^old-folder1/(.*)$ /new-folder1/$1 [R=302,L]
However, if possible for the moment I still want to serve images from the old structure ie.
www.example.co.uk/old-folder1/old-images-folder/old-image.jpg
At the moment the above is being rewritten as:
www.example.co.uk/new-folder1/old-images-folder/old-image.jpg
Which makes sense but this leads me to my question, is there any way of excluding some of the sub directories in 'old-folder1' from the RewriteRule above so that for example www.example.co.uk/old-folder1/old-images-folder/old-image.jpg is still accessible?
I don't have much experience with this but from research I came up with the following but it doesn't work.
RewriteCond %{REQUEST_URI} !^/old-folder1/old-images-folder/.*$
I am beginning to think this might not even be possible with the approach I have taken with:
RewriteRule ^old-folder1/(.*)$ /new-folder1/$1 [R=302,L]
Thanks
Entire contents of the .htaccess file at the moment is:
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/old-folder1/old-images-folder/.*$
RewriteRule ^old-folder1/(.*)$ /new-folder1/$1 [R=302,L]
Try:
RewriteRule ^old-folder1/(?!old-images-folder)(.*)$ /new-folder1/$1 [R=302,L]
HOwever, what's probably happening is that your images are linked using relative URLs, so they may be relatively linked to the "new" images folder.
You should check your links and make sure they're linked correctly to the old path.

htaccess rewrite query string nothing works

THE PROBLEM
After looking at 50+ StackOverflow posts and trying many permutations of my htaccess file, it does nothing still.
WHAT I HAVE TRIED
Using this website to generate my htaccess file: http://www.generateit.net/mod-rewrite/
Setting AllowOverride All in my httpd.conf file and restarting Apache.
MY CURRENT HTACCESS FILE
Lives in the root directory.
RewriteEngine On
RewriteBase /
RewriteRule ^find-a-local-doctor/([^/]*)/([^/]*)$ /find-a-local-doctor/?state=$1&city=$2 [L]
WHAT I WANT TO ACCOMPLISH
Change this URL:
http://www.md1network.com/find-a-local-doctor/?state=FL&city=Tampa
To this:
http://www.md1network.com/find-a-local-doctor/FL/Tampa
ADDITIONALLY
Since the actual file doing the work is: http://www.md1network.com/find-a-local-doctor/index.php, I need to be able to parse the query string with PHP. Hopefully, I will still be able to do this to get the state and city.
Please help.
Thanks.
Your existing rule looks alright but you will need an additional external redirection rule for reverse. Put this rule before your existing rule (just below RewriteBase /).
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(find-a-local-doctor)/(?:index\.php)?\?state=([^&]+)&city=([^&\s]+) [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]

modrewrite - all php files to html which is SEO friendly and works in both ways

I'm new to this modrewrite beast. I managed to get it working and I can normaly load index.php as index.html for example.
Problem is when I try to navigate to index2.html and I get error 404 error, The requested URL /index2.php was not found on this server.
I have this code in my .htaccess:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.html$ $1.php [nc]
I've tryed few things, but I just get caught in the loop.
Basicaly I want rewriting from PHP to HTML, but I want to see the files which are actually HTML work aswell.
My 2nd question, perhaps most important is, how good is that in SEO terms, is it just as normal or are there any downsides of using such rewrite rules?
Try adding a condition for -f
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(.*)\.html$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)\.html$ $1.php [nc]
This checks to make sure the the file exists as a php before rewriting
As for your second question, it doesn't make a big difference either way, whether extensions are html or php. Its mainly so it appears you're serving static content.

Change Displayed URL Structure using mod_rewrite NOT Working

I need to change the structure of the displayed client-side URL. I'm not too skilled using regex and coding for the .htaccess file. Basically, I have a structure that looks something like:
http://www.example.com/catalog/index.php?cat=lt&sec=lt1-1&id=nmlt10.
I would like this to be displayed in the address bar as:
http://www.example.com/catalog/lt/lt1-1/nmlt10.
This is what I came up with, but it has had no effect:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\$ /catalog/index.php?cat=$1&sec=$2&id=$3 [L]
I tested and removed any other rules in the .htaccess file to ensure nothing was being overwritten. I'm on a shared hosting apache server, and know that mod_rewrite is enabled, because I use it to rewrite non-www to www urls. I don't receive and 500 error messages, I just do not notice any change at all. I'm not sure where I'm going wrong here, so hopefully someone can point me in the right direction.
Finally found a solution that worked:
RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
Appreciate LazyOne's response to get me on the right track; however, when using:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
I wasn't able to following links that were already placed on the site, it treated different directories as the variables, for example, when browsing to an image or file, say:
folder/folder/image.png
It would grab "folder" - "folder" - and "image" as the variables. I can see why that was happening, if anyone has a different solution or an explanation, please let me know, I'm always willing to learn.
Since your .htaccess is in website root folder, then you should use thus rule:
RewriteEngine On
RewriteBase /
RewriteRule ^catalog/([^/]+)/([^/]+)/([^/]+)$ /catalog/index.php?cat=$1&sec=$2&id=$3 [QSA,L]
If you place it in .htaccess in /catalog/ folder, then you can remove catalog from it:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
I have tested rule before posting -- works fine for me.
This rule (same as above) will check if URL is a file or folder and will only rewrite if it is not:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]

Resources