Cleaning urls my htaccess - apache

I have looked for answers but none have worked for me. I want to clean up my urls removing the page name, extension and variables on all my pages in all folders.
so
http://www.example.co.uk/home/index.php?username=ben&id=1
to
http://www.example.co.uk/home/
and
http://www.example.co.uk/profile/index.php?blog=test&id=45
to
http://www.example.co.uk/profile/
ect....
im running apache
thanks
sorry for late reply. so far i have
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([a-z.]+\.)?example\.co\.uk$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? http://www.example.co.uk%{REQUEST_URI} [R=301,L]
To force the www. and also remove the file name and extension

You need to enable mod rewrite module in your apache configuration.
In your .htaccess file try some basic rewrite rule.
If you rewrite
http://www.example.co.uk/home/index.php?username=ben&id=1
to
http://www.example.co.uk/home/
you are losing the parameters "username=ben&id=1".
Some rewrite tutorials from google sesarch
https://www.google.com/search?q=mod+rewrite+tutorial&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a&channel=fflb
http://www.sitepoint.com/guide-url-rewriting/
http://net.tutsplus.com/tutorials/other/a-deeper-look-at-mod_rewrite-for-apache/

Related

Rewrite URL using .htaccess by replacing /? with?

How can I rewrite URL (i.e. remove last / after test) using .htaccess on php page from
from www.example.com/test/?sku=23456&qty=3 to www.example.com/test?sku=23456&qty=3
from www.example.com/page2/?page=3&emp=543 to www.example.com/page2?page=3&emp=543
from www.example.com/stream/?start=4&id=tdfcs45s&q=sat to www.example.com/stream?start=4&id=tdfcs45s&q=sat
I tried but it doesn't work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
With your shown samples and attempts please try following .htaccess rules file. We need to use THE_REQUEST variable here of apache. Check this if this helps you, also clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s(/test)/(\?sku=(\d+)&qty=\d+)\s [NC]
RewriteRule ^ %1%2 [L]

.htaccess Messy Rewrite Rules

I could do with some assistance trying to clean up an .htaccess file that has been running on one of our servers at work.
It is currently set up so that if someone types example.com it will redirect to www.example.com. The problem is that we want to utilise subdomains but when we try to add a subdomain, like beta.example.com it will redirect to www.beta.example.com
Here is the .htaccess file
DirectoryIndex index.html index.php
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !www.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
I have tried multiple variations from questions here on SO and via Google and htaccess generators and none of them seem to help as they usually put the site in a redirect loop.
Any help on getting the configuration correct would be appreciated.
-- Chris
To automatically add a www to your domain name when there isn't a subdomain, add this to the htaccess file in your document root::RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]

htaccess rewrite after redirect

I have a site which uses htaccess to rewrite all pages to the index page with a hash which is then used to serve up content. The file looks like this....
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ /index.php?urlpath=$1 [NC,L,QSA]
I am now moving some of the pages of the site, however if I add a redirect such as....
Redirect 301 /blog /new_location/blog/
I am running into problems with the resulting url looking like
https://mydomain/new_location/blog/urlpath=blog.php
Can anyone suggest a way that I get the page to redirect to mydomain/new_location/blog/ and then run the rewrite on the new url.
Many thanks
RewriteRule and Redirect are from different Apache modules, so run at different times in the processing, not in the order they appear in the configuration. You're best off sticking to one module or the other, by using the [R] flag to RewriteRule.
RewriteRule /blog(.*) /new_location/blog$1 [R=301]
OK, I managed to get this working using a combination of Redirect and Rewrite like so....
Redirect 301 /blog /new_location/blog
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/blog
RewriteRule ^([^?]*)$ /index.php?urlpath=$1 [NC,L,QSA]
Maybe not the neatest solution, but it work!

how to rewrite URL using .htaccess file?

I am having problem in rewriting a url using .htaccess file. I am not aware of regex. I have red so many blogs and used online rewrite rule generator to generate url and pasted in .htaccess file. But no one is worked. I have enabled mod_rewrite module in httpd.conf file in apache server.
I want to rewrite a below url.
I have developed project in OS - Win7 with Apache server.
http://localhost:81/quora/tamilnanban/innerpage.php?url=why-you-should-avoid-watching-porn-on-android-smartphones
to
http://localhost:81/quora/tamilnanban/why-you-should-avoid-watching-porn-on-android-smartphones
my current .htaccess file contains below codes:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php
#RewriteRule ^url/([^/]*)\.html$ :81/quora/tamilnanban/innerpage.php?url=$1 [L]
RewriteRule ^/quora/tamilnanban/([^/]*)\.html$ /quora/tamilnanban/innerpage.php?url=$1 [L]
This above rule also not working. Kindly help me to achieve url rewriting using .htaccess file. Also if i am doing any mistakes in the above steps, please correct me. Thanks in advance.
To convert /quora/tamilnanban/innerpage\.php\?url=foobar" to /quora/tamilnanban/foobar you can use the following rules in /quora/tamilnanban/.htaccess :
RewriteEngine on
#1)Redirect "/quora/tamilnanban/innerpage\.php\?url=foobar" to "/quora/tamilnanban/foobar"#
RewriteCond %{THE_REQUEST} /quora/tamilnanban/innerpage\.php\?url=(.+)\sHTTP [NC]
RewriteRule ^ /quora/tamilnanban/%1? [L,R]
#2)The rule bellow will internally map "/quora/tamilnanban/foobar"" to "/quora/tamilnanban/innerpage\.php?url=foobar"#
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.+)$ /quora/tamilnanban/innerpage\.php?url=$1 [L]

Rewriting folder won't work

I have an app stored on my server in "SERVER_ROOT/app/1.0.1/index.php". The app is accessed by typing in "example.com/index.php" and I use a rewrite to achieve this.
The issue I am having is I want users to access "SERVER_ROOT/app/1.0.1/image/" by typing "example.com/image/" but none of my rewrites work when I edit the .htaccess file.
Here is my .htaccess file:
Options -MultiViews
RewriteEngine on
RewriteBase /app/1.0.1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+) index.php/$1 [L]
RewriteRule ^image/$ app/1.0.1/image/ [NC,L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Thanks,
Peter
Since the /image/ directory doesn't actually exist, it'll get trapped by the first rule that routes everything to index.php. Assuming your htaccess file is in your document root, you need to add the rule above your routing rule and change it to this:
RewriteRule ^image/(.*)$ image/$1 [L]
If your htaccess file isn't in the document root, then you need to add this rule to the top of the htaccess file in your document root:
RewriteRule ^image/(.*)$ /app/1.0.1/image/$1 [L]