.htaccess Rewrite URL as subdirectory - .htaccess

I have this url:
http://mysite.com/home.php?name=username
I want to be able to access that by going to:
http://mysite.com/home/username
Is there a way to do that?

You can do this using the mod_rewrite extention that comes with Apache.
Create a new .htaccess file in your web root directory, and populate it with the following text.
RewriteEngine On
RewriteRule ^home/([^/]*)$ home.php?name=$1 [L]

Related

htaccess redirect from folder with subdirectory to single path directoy

Previously my site was in wordpress, now converted into html.
I have 500+ pdfs that needs to redirect from
mysite.com/wp-content/themes/my-theme/pdfs folder to mysite.com/pdf/
Do you have server-level access to the site? If so I would create a symbolic link in the public root directory of your website to wp-content/themes/my-theme/pdfs and call it pdf.
If all you can do is .htaccess, then you can create a file like this:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^pdf/(.*)$ /wp-content/themes/my-theme/pdfs /$1 [R=301,NC,L]
You might check out a great tutorial on rewrite rules.
I tried like this it works for me.
RewriteRule ^wp-content/themes/my-theme/pdfs/(.*)$ /pdf/$1 [R=301,NC,L]

Amend .htaccess path rewrite to include subdirectory

I currently have the following .htaccess rewrite rule setup on my site:
RewriteEngine On
RewriteRule ^([^/d]+)/?$ index.html?id=$1 [QSA]
The rule works in such a way that if I go to the following URL:
http://example.com/dashboard
It won't try and find the dashboard directory that doesn't exist but instead it will keep the URL as is and redirect the user to the root index page. From there I just use javascript to control what view the user will see depending on what path is appended.
The code works exactly as I want it to but i've now had to move my site into a sub-directory on our server. The URL structure is now this:
http://example.com/mysubdir/dashboard
I tried rewriting my rewrite rule to incorporate the directory but have not been successful so far as i'm no .htaccess expert. I tried something along the likes of:
RewriteEngine On
RewriteRule ^([^/d]+)/mysubdir/?$ index.html?id=$1 [QSA]
Could anyone tell me how I can amend my rewrite rule to work in my sub-directory?
You were close - this should do it:
RewriteEngine On
RewriteRule ^mysubdir/([^/d]+)/?$ /mysubdir/index.html?id=$1 [QSA]
Demo here: http://htaccess.mwl.be?share=6e574cc6-90a4-54ca-b113-ce72d6eb5203

Changing stylesheet url using .htaccess 2

I have very specific question, I want to solve it using .htaccess and mod_rewrite, I want to make rule in .htaccess file using mod_rewrite, so when somebody visit my site ex.
mysite.com , mysite.com/css/style.css file should be read from other location mysite.com/version1/css/style.css, if I say it otherwise, style.css should not be "picked up" from root folder, but sub-folder.
Try adding this to the htaccess file in your document root:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com$ [NC]
RewriteRule ^css/(.*)\.css$ /version1/css/$1.css [L]

htaccess 301 redirect with wildcard to a single page

We took over a website with about a kabillion pages in the old site root directory done in htm that need to be retired. I want to do a 301 redirect from the pages to the index.php in the root directory of the new site using a wildcard. An example of the page naming structure follows:
oldpage_dees.htm
oldPage_dat.htm
oldPage_deeudderting.htm
and so on. As stated, I need them redirected to the index.php in the root directory. Going by examples and discussions here I've tried:
RewriteEngine On
RewriteRule ^/oldpage_([\w]*).htm$ /index.php [R=301,L]
but I get a 404 error.
Any suggestions? Thanks in advance!
As .htaccess is directory level configuration file, you don't need to specify forward slash, I think this will do the job:
RewriteEngine On
RewriteRule ^oldpage_([\w]*).htm$ index.php [R=301,L]
Meanwhile, you can use the following .htaccess tester to debug your rewrite rules.

How to redirect any folders within a subdirectory to a specific file using htaccess?

I want to redirect any folder to a specific php file with the folder name as variable using htaccess, for example:
www.domain.com/subdirectory/folder1 redirects to www.domain.com/subdirectory/file.php?id=folder1
www.domain.com/subdirectory/folder2 redirects to www.domain.com/subdirectory/file.php?id=folder2
This should work if the folders have "/" at their end, for example,
www.domain.com/subdirectory/folder3/ redirects to www.domain.com/subdirectory/file.php?id=folder3
It will be better if I can put the .htaccess file in the subdirectory and get redirect rule according to it.
I created a .htaccess file with the following code while putting the file in the subdirectory:
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*)$ file.php?id=$1
but it does not work.
RewriteRule ^folder(.*)$ file.php?id=$1
use these i am not tested but it will be work.

Resources