Mod_Rewrite with Relative Path Redirects - linux

I have this rule in an .htaccess file located in a directory named clips/:
RewriteRule ^mlk/?$ segment/index.php?clip=1 [R=301,QSA,L]
What I am intending is that when someone visits http://example.local/clips/mlk they are redirected to http://example.local/clips/segment/index.php?clip=1
What actually happens is that when someone visits example.local/clips/mlk they are redirected to example.local/var/www/example/clips/segment/index.php?clip=1
I'm not sure why it's doing this. If I change the rewrite rule to this:
RewriteRule ^mlk/?$ /segment/index.php?clip=1 [R=301,QSA,L]
The user is redirected to example.local/segment/index.php?clip=1, which is still incorrect. I don't want to have to specify an absolute path in the case of these files being moved around the website's directory tree. How can I get this to work relatively instead of absolutely?

Try adding a RewriteBase directive as below
RewriteEngine On
RewriteBase /clips/
RewriteRule ^mlk/?$ segment/index.php?clip=1 [R=301,QSA,L]
EDIT
but is there any way to get this to work without using a RewriteBase directive
You could also try
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(/[^/]+/) [NC]
RewriteRule ^mlk/?$ http://%{HTTP_HOST}%1segment/index.php?clip=1 [R=301,QSA,L]

Related

.htaccess rewrite for subdomain only

I have the following .htaccess code that successfully rewrites subdomain directories. So if a user visits jim.domain.com they get served placeholder.php?page_type=profile. However, this also affects the main directory http://domain.com. I'd like it so that anything with a subdomain is directed to the placeholder.php page, but using the main directory with no subdomain would act as usual, going to the index.php in the main directory. How would I achieve this? Thanks for any advice.
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^exclude.domain.(.*)
RewriteCond %{HTTP_HOST} !^www.domain.(.*)
RewriteCond %{HTTP_HOST} (.*)\.domain\.com
RewriteRule ^$ placeholder.php?page_type=profile [L,QSA]
If your wanting all requests for test.domain.com to go to domain.com/placeholder.php?page_type=profile as in your comment then you need to include the domain in the rewrite
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^exclude.domain.(.*)
RewriteCond %{HTTP_HOST} !^www.domain.(.*)
RewriteCond %{HTTP_HOST} (.*)\.domain\.com
RewriteRule ^$ http://domain.com/placeholder.php?page_type=profile [L,QSA]
this will not how ever deal with this part
but using the main directory with no subdomain would act as usual, going to the index.php in the main directory
because you have no file/directory miss rules that redirect to index.php. Do you need those, or really just want to solve the first part?
I also wonder if your wanting profile replaced with the subdomain match, in which case you want
RewriteRule ^$ http://domain.com/placeholder.php?page_type=%1[L,QSA]
but both of these examples will not rewrite any path of test.domain.com/longer_url because the ^$ is only matching an empty URL path. But how you want to handling paths needs to be decided before a suitable Reerite can be proposed.

Using .htaccess to change directory in url

I am trying to change the url that is displayed in the address bar from mysite.com/blog/wedding-hair/ to mysite.com/services/wedding-hair/ using .htaccess.
Using answers from:
https://stackoverflow.com/questions/8713319/assigning-different-name-to-existing-folder-in-url-in-htaccess
rewrite a folder name using .htaccess
Replace directory name in url with another name
I added to the .htaccess file. Here is the .htaccess file, I added the last rewrite rule:
Options -Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com$
RewriteRule ^/?$ "http\:\/\/www\.mysite\.com" [R=301]
RewriteRule ^blog/(.*)$ /services/$1 [L]
the non-www redirect works but not the blog-services rewrite. I thought maybe I had the directory names reversed but changing them around doesn't work either. I have tried adding and removing /'s around the directory names in all of the different combinations. I tried adding
RewriteCond %{THE_REQUEST} ^GET\ /blog/
before my RewriteRule. Nothing I Have tried has worked, the displayed url remains mysite.com/blog/wedding-hair/
I am sure this is pretty straight forward for someone but I am unable to get this correct. Any help would be appreciated.
When I was working on this yesterday I didn't think about the fact that the blog directory is a WordPress install. Here is the .htaccess file that is in the blog directory:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress
I have tried adding my RewriteRule in this file but still no joy.
The problem here is that RewriteRule ^blog/(.*)$ /services/$1 [L] internally rewrites the URI, so that the browser doesn't know it's happening, this happens entirely on the server's end. If you want the browser to actually load a different URL, you need to use the R flag like you are in your www redirect, though it's only redirecting requests to root. If you want it to redirect everything to include the "www", you want something like this:
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Then to redirect "blog" to "services", just add the R flag (or R=301 if you want the redirect to be permanent).
RewriteRule ^blog/(.*)$ /services/$1 [L,R]
And, if for whatever reason your content isn't actually at /blog/, you need to internally rewrite it back
RewriteCond %{THE_REQUEST} ^GET\ /services/
RewriteRule ^services/(.*)$ /blog/$1 [L]
But this is only if your content is really at /blog/ but you only want to make it appear that it's at /services/.
Actually, in such case, as you have a specific field in Wordpress options to handle the display of a different url, it CAN'T work with .htaccess is the WordPress rules are executed at the end.
And it would be much simpler to use the field "Site Address (URL)" in the General Settings, and enter "mysite.com/services/"
If you don't do that, in spite of your .htaccess, the WP internal rewriting will use you installation repertory

Redirect a .jpg using .htaccess

I needed to redirect "example.com/image/img.jpg" to "example.com/view/img.jpg"
Therefore I've used
RewriteRule (^|.*?/)image/(.*)$ /$1view/$2 [R=302,L,NC]
But, still if I try to visit "example.com/image/img.jpg" it won't redirect. What am I doing wrong?
Thank you.
I needed to redirect "example.com/image/img.jpg" to "example.com/view/img.jpg"
Apparently the above examples are not accurate as your rule shows directories image and view can be at any level in the corresponding URL directory structures.
If that's the case, you may try this in one .htaccess file at root directory:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/(.*)?image/([^.]+)\.jpg [NC]
RewriteRule .* /%1view/%2.jpg [R,L,NC]
In case image and view directories are indeed at the first level as described in the examples, replace the last 2 lines with this one:
RewriteRule ^image/(.*) /view/$1 [R,L,NC]
Replace [R,L,NC] with [R=301,L,NC] for permanent redirection or with [L,NC] for internal mapping.

mod rewrite doing something weird

http://localhost/clean_urls/user/whtffgh redirect fine to this http://localhost/clean_urls/user/whtffgh/ (pretty much anything I put there works).
http://localhost/clean_urls/user/cohen however, redirects to this
http://localhost/clean/ and I have no idea why. It should just add a /
Heres my .htaccess code
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /clean_urls/user/$1/ [L,R=301]
RewriteBase /clean_urls/user/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9]+)/$ index.php?user=$1
</ifModule>
UPDATE:
The code works besides when I put cohen as the username. The second part of the .htaccess file is making user/index.php?user=username look like user/username so its just a $_GET variable that looks nice in the url and is SEO friendly (apparently).
I am just learning this stuff and making a webapp to try it out so not really sure where its going but I do think I will need another variable on the end. http:://localhost/clean_urls/user/fred/someverb
You misunderstand what the RewriteBase directive is used for. It helps the rewrite engine in a "per directory" context to convert the relative URIs to the correct filename equivalent.
You should only have one RewriteBase directive per .htaccess file. The rewrite engine by default will use the lowest .htaccess file on the path with the RewriteEngine On directive set.
So in DOCroot (that is the directory where a "GET /xxx" is mapped to, this should be
RewriteBase /
If your .htaccess file is in DOCroot/clean_urls/user then it should have
RewriteBase /clean_urls/user/
How do you want to decode URIs of the form http:://localhost/fred or are you only wanting do process URIs of the form http:://localhost/clean_urls/user/fred/. What about http:://localhost/clean_urls/user/fred/someverb?
Let me know and where you are putting you .htaccess then I can give you the right content.

.htaccess redirect

i want to create a clean url, as in, let say, i have a site www.mywebsite.com, i want, if anybody tries http://www.mywebsite.com/something, he should get redirected to www.mywebsite.com,
so any thing that is typed ahead of mywebsite.com/ they should get redirected to www.mywebsite.com
how can i do this in .htaccess, kindly help.
RewriteEngine ON
RewriteRule ^/.+ / [L,R]
That will redirect everything to / as a permanent redirect. Look at http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html for more information on the flags you can add for transparency etc etc.
If you want this in .htaccess make sure you have the correct override directives in your <VirtualHost > config.
If you want to only redirect if there files and directories don't exists already, then this will work:
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/.+ / [R,L]
If you never want to have sub-content accessible then remove the condition rules
RewriteEngine ON
RewriteRule ^/.+ / [R,L]

Resources