.htaccess rewrite /dir/sub to /dir - apache

I have files in /directory/subdirectory and I'd like it to appear to users that they're in /directory.
Here's what I have in my .htaccess file at the moment (returns 500 error):
RewriteEngine on
RewriteRule ^directory/(.*) directory/subdirectory/$1 [NC,L]
I know that rewritebase could accomplish this if my files were in the root www directory but I can't do that for this project.
Any ideas?
Thanks!

You need to add [NC,L] at the end of your rewriterule line. NC indicates that this rule is case insensitive, and L indicates Last, that's where the engine stops processing the rules.
For more info visit ModRewrite cheatsheet

Use this rule:
RewriteRule (?!^directory(/.*|)$)^(directory)/(.*)$ $1/subdirectory/$2 [NC,L]

Related

.htaccess redirection from multiple directories to a single page

I have a single blog.php page that should get redirected from all these requests:
www.site.com/blog #goes to blog.php
www.site.com/blog/id-of-an-entry #goes to blog.php?id=id-of-an-entry
Also internationalised versions such for french:
www.site.com/fr/blog *# to blog.php?lang=fr
www.site.com/fr/blog/id-of-entry* #to blog.php?lang=fr&id=id-of-entry
What would be the more eficient and effective cond/rules for .htaccess? I've made many attemps but end walking in circles or with to many specialised rules :-) Thanks for any insights!
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 ^blog/([^/]+)/?$ /blog.php?id=$1 [L,NC,QSA]
RewriteRule ^blog/?$ /blog.php [L,NC,QSA]
RewriteRule ^([^/]+)/blog/([^/]+)/?$ /blog.php?lang=$1&id=$2 [L,NC,QSA]
RewriteRule ^([^/]+)/blog/?$ /blog.php?lang=$1 [L,NC,QSA]
I am not using this regulary so there can be better way but this could help you. Rules works like this. In () are your variables which corresponds to what you want to match. Than you can refer to them by $1 and so on. For example news/local would redirect them to file news-local.php you can use the same system to match you get variables.
RewriteRule ^([a-z]+)/([a-z]+)$ $1-$2.php

.htaccess rewrite folder-like url to existing html page

My local project's root is localhost/website
I created the file localhost/website/.htaccess with the following contents:
RewriteEngine On
RewriteRule ^content/(.*)$ content/$1.html [L]
What I exactly want to achieve:
localhost/website/main -> localhost/website/content/main.html
where localhost/website/content/main.html actually does exist.
I tried several rules but none of them worked for me. Somehow I think I'm missing something.
If you want to redirect /main to /content/main you'd have to omit the content/ from the first argument.
The following does the trick
RewriteEngine On
RewriteCond %{REQUEST_URI} !^.*content/[^/]*\.html$
RewriteRule ^(.*)$ content/$1\.html [L]

URL Rewriting using .htaccess

To change the URL /mobiles.php?id=5 to /mobiles/5
The content of .htaccess file is as follows:
Options +FollowSymlinks
RewriteEngine on
RewriteRule /mobiles/$1 ^/mobiles.php?id=([0-9]+)$
But still it is showing /mobiles.php?id=5 in the address bar. Please help. Is there anything else needs to be added in the .htaccess file?
Note:
mod_rewrite module is enabled
I have restarted Apache server after making changes to the .htaccess
file
.htaccess file is in htdocs folder of Apache.
I am using Windows + PHP + Apache + MySQL
This works for me:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^mobiles/([0-9]+)$ mobiles.php?id=$1&rew [L]
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^mobiles.php$ /mobiles/%1? [R,L]
If you see this line:
RewriteRule ^mobiles/([0-9]+)$ mobiles.php?id=$1&rew [L]
I have added rew variable in the query string to prevent Apache to fall in an infinite loop
When Apache execute this line:
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
Is to make sure that url has not been rewritten for Apache
If your only concern is that the old url stays in the address bar, and you want this not to happen, try adding an [R] at the end.
RewriteRule ^/mobiles.php?id=([0-9]+)$ /mobiles/$1 [R]
Did you actually see the correct page?
By the way, the rewrite rules generally go the other way. I would be expecting to see something like:
RewriteRule ^/mobiles/([0-9]+)$ /mobiles.php?id=$1
Is your concern one of making sure a URL with query parameters does not show up in the address bar?
If I understand correctly, you want
Internally redirect /mobiles/5 to /mobiles.php?id=5
Also redirect the browser TO /mobiles/5 if a user navigates to /mobiles.php?id=5
For this you need 2 rules one to internally rewrite the URL for 1st case and 2nd for browser redirection.
You can do it like this:
RewriteEngine on
# for internal rewrite
RewriteRule ^/?mobiles/([0-9]+)/?$ /mobiles.php?id=$1 [L]
# for browser redirect
RewriteRule ^/?mobiles\.php\?id=([0-9]+)$ /mobiles/$1/ [R,L]
You are doing the opposite, should be:
RewriteRule ^/something/mobiles/([0-9]+)$ /something/mobiles.php?id=$1

Rewriting a number based URL using .htaccess RewriteRule

How can I rewrite a simple number based URL to a sub folder?
I want http://mysite.com/123 to redirect to http://mysite.com/en/123.
The number could be anything from 1 - 9999. My attempt in htaccess was:
RewriteRule ^([0-9]+)$ /en/$1/ [R]
But that doesn't work.
your syntax is right, I just remove slash in the end of line and it works:
RewriteRule ^([0-9]+)$ /en/$1
Make sure that this 3 lines are writen in your HtAccess File (sorry for mybad english)
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
##
## Now the rewrite
RewriteRule ^([0-9]+)$ ./en/$1

Apache RewriteRule not working without Page # specified

I have a rewrite rule set up in my .htaccess file:
RewriteRule ^Crocodile-Style/([0-9]+)/?$ products/display.php?folder=crocodile-style&page=$1 [L,NC]
http://test.bradp.com/drupal/Crocodile-Style/1 works OK.
http://test.bradp.com/drupal/Crocodile-Style/ DOES NOT WORK.
Apache throws a 404. The PHP logic defaults to page 1 without a page specified, so I know the script is fine.
What can I do?
Thanks
Nick
It's probably easiest to implement this with two rules:
RewriteRule ^Crocodile-Style/?$ products/display.php?folder=crocodile-style [L,NC]
RewriteRule ^Crocodile-Style/([0-9]+)/?$ products/display.php?folder=crocodile-style&page=$1 [L,NC]