Apache htaccess URL rewrite - apache

I have a URL like -
http://www.mydomain.com/website/dev/main/html/about_us.html
How can I rewrite it to simply http://www.mydomain.com/about ?
I have seen many examples of URL rewrite on internet and SO but still couldn't figure it out. Any help would be appreciated.

Try this in your htaccess, make sure that mod_rewrite is actuvated on your server:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^about$ ./website/dev/main/html/about_us.html

Working from the Apache Rewrite Guide, your .htaccess file should contain:
RewriteEngine on
RewriteRule ^/about$ ./website/dev/main/html/about_us.html [R]
That turns the rewrite engine on (if it isn't already) and then sets up a rule. Any URL after the domain that matches the regex ^/about$ will be redirected transparently to /website/dev/main/html/about_us.html

I'm sure this is a repeat of an existing question. Make sure you have in your file:
RewriteEngine On
RewriteBase /
RewriteRule ^about$ website/dev/main/html/about_us.html
You can also pass vars this way too:
RewriteRule ^about/(.+)/(.+)$ about/index.php?q=$1&p=$2
Where about/12/23 would equal about/index.php?q=12&p=23

Related

Redirection in .htaccess for files in subdirectory, but not root of subdirectory

I am having trouble finding the correct way to write a .htaccess redirection.
I want to invisibly redirect any page in a subdirectory:
http://my-site.com/test/any_page.php
to
http://my-site.com/AIMS/rewrite.php
However, I don't want to redirect the root subdir:
http://my-site.com/test/
The above url should not redirect. The problem with my rule is it redirects any page, and the root of the subdir as well.
I was unable to find any questions already asked for this specific case. Many thanks in advance for any tips on what I might be doing wrong.
Here are my rules:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^my-site.com$ [NC]
RewriteRule ^(.*)$ http://my-site.com/$1 [L,R=301]
RewriteRule ^test/(.*)$ /AIMS/rewrite.php?data=$1 [NC,L]
</IfModule>
In your rewrite you're using the regex .* which means match zero or more characters - you want to match one or more.
Changing it to the below should do the trick:
RewriteRule ^test/(.+)$ /AIMS/rewrite.php?data=$1 [NC,L]

Simple .htaccess rewrite rule but doesn't work properly

I've trying to rewrite one url but something doesn't work as must and I get the massage
Not Found
The requested URL /1/1.html was not found on this server.
This is what I have in .htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^/([^/]*)/([^/]*)\.html$ /view.php?id=$1&name=$2 [L]
And this is the href link for this case
href="/'.$row['id'].'/'.$row['name'].'.html"
Any idea why is this?
You must remove leading slash in your rule
RewriteRule ^([^/]+)/([^/]+)\.html$ /view.php?id=$1&name=$2 [L]
You need a leading slash in your rules only if you write it directly in httpd.conf instead of .htaccess files.
You also need it until Apache 2.4 if i don't make a mistake

Mod_rewrite - url rewriting isn't working

I've been trying to get the mod_rewrite in my .htaccess file to work for the past few hours but I can't seem to pull it off. The .htaccess files definitely work on my server because I've used them before.
I've tried to rewrite it with this generator and testing it with this tool as well as on my server.
URL
I need to turn
http://someurl.com/news-detail.html?id=10&news=this-is-an-article
into
http://someurl.com/news/10/this-is-an-article
or
http://someurl.com/10/news/this-is-an-article
Rewrite
I've tried:
RewriteEngine On
RewriteRule ^([^/]*)/news/([^/]*)$ /news-detail.html?id=$1&news=$2 [L]
and
RewriteEngine on
RewriteRule /(.*)/news/(.*) news-detail.html?id=$1&news=$2 [L]
and
RewriteEngine on
RewriteRule news/([0-9]+)/([a-zA-Z_]+)$ news-detail.html?id=$1&news=$2 [L]
and many others.
I hope someone can help me out here..
Kindly check/add RewriteBase in your .htaccess file.
Also check if htaccess is allowed to rewrite or not.
Syntax is below
RewriteEngine on
RewriteBase /flodername/
RewriteRule ^urlinbrowser/(.*)/(.*)$ filename.php?first_param=$1&secondparam=$2 [L]
Keep your rules like this in root .htaccess:
RewriteEngine On
RewriteBase /dev/hoig/
RewriteCond %{THE_REQUEST} /news-detail\.html\?id=([^\s&]+)&news=([^\s&]+) [NC]
RewriteRule ^ %1/news/%2? [R=302,L]
RewriteRule ^([^/]+)/news/([^/]+)/?$ news-detail.html?id=$1&news=$2 [L,NC,QSA]
This will support /10/news/this-is-an-article URI structure for pretty URLs.

Rewrite URLs with .htaccess but ignore specific Directories

I have a directory on a site:
http://example.com/directory/
In it I have a .htaccess file.
I want it to take any URL like this:
http://example.com/directory/section/day/
And rewrite it to:
http://example.com/directory/index.php?arg1=section&arg2=day
Except for any URLs that refer to these directories:
http://example.com/directory/css/
http://example.com/directory/javascript/
http://example.com/directory/images/
I have the first part working, but unable to tell it to exclude files in certain directories:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([^/]+)/([^/]+) index.php?arg1=$1&arg2=$2 [NC]
Update:
This works in a very basic and simple sense:
RewriteRule ^css - [L,NC]
RewriteRule ^javascript - [L,NC]
RewriteRule ^images - [L,NC]
RewriteRule ^([^/]+)/([^/]+) index.php?arg1=$1&arg2=$2 [NC]
but I'm sure there is a more elegant solution?
I think the keyword you're looking for is RewriteCond. It's pretty similar to what you ended up with.
RewriteCond %{REQUEST_URI} !^(css|javascript|images)
RewriteRule ^([^/]+)/([^/]+) index.php?arg1=$1&arg2=$2 [NC]
You might be able to use RewriteCond to check if an actual file exists... at least, I use this for websites when I want things like CSS, Javascripts, and images to be accessible (without the request being redirected.)
Here's the line I use:
# only rewrite if the requested file doesn't exist
RewriteCond %{REQUEST_FILENAME} !-s
Let me know if that works for you!

.htaccess in subdomain

I'm trying to set up a subdomain as follows:
http://subdomain.domain.com should load what is really http://subdomain.domain.com/index.php/contest/
http://subdomain.domain.com/stepone should load what is really http://subdomain.domain.com/index.php/contest/stepone
http://subdomain.domain.com/images/example.jpg should load http://subdomain.domain.com/images/example.jpg as normal.
I've had this all set up and working in a subdirectory on the main domain before but now that I'm trying to do it on a subdomain it's stopped working.
Here is my .htaccess:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|stylesheets|javascript|email|robots\.txt|test\.php|terms\.html)
RewriteRule ^(.*)$ /index\.php/contest/$1 [L]
Please help me!
Are rewrite rules working at all? I.e. do you have AllowOverride All or something for virtual hosts, as per:
http://www.webhostingtalk.com/showthread.php?t=481815
Also, take a look this comment by Gumbo on this SO question:
Htaccess help, $1 being used before its set??? what?
The Drupal .htaccess file is a good example of mapping /?q=query to /query, but not redirecting things which provide an explicit file/directory match. Here's the relevant snippet from Drupal's .htaccess with ?q= changed to index.php/contest/.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/contest/$1 [L,QSA]
If you need to restrict the domain so that it doesn't redirect normally but just from the one domain, insert before the RewriteRule this:
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$ [NC]
I think I have found the answer using:
RewriteRule ^(.*)$ subdomain.domain.com/index\.php/contest/$1 [L, P]
Is there any reason I shouldn't use the force proxy like this? (Does it slow things down?)
Thanks for your answers guys.