how to rewrite URL using .htaccess file? - apache

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]

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]

Apache RewriteRule not working – page not found

I have no idea why it doesn't work, /spelling/30000 gives the Not Found page. Please help.
RewriteEngine On
RewriteBase /
RewriteRule ^/([0-9]+)$ /?mod=spelling&word=$1 [PT]
RewriteRule ^$ /?mod=spelling [PT]
With your shown samples please try following .htaccess rules file. Considering that index.php file is the one which is taking traffic in backend for internal rewrite rules. Please make sure to keep your .htaccess file along side with index.php file.
Also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /
RewriteRule ^/?$ index.php?mod=spelling [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ index.php?mod=$1&word=$2 [QSA,L]

htaccess remove folder redirect

I have a problem removing folders from an url. I want that google / old links aren't broken. The old webpage had several sections with a structure like this
example.com/news/items/entry1.html
example.com/news/items/entry2.html
example.com/blog/items/foo.html
The new page has the urls like this:
example.com/news/entry1
example.com/news/entry2
example.com/blog/foo
Removing html was rather straight forward
<IfModule mod_rewrite.c>
RewriteEngine On
# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php [QSA,L]
RewriteCond %{THE_REQUEST} /([^.]+)\.html[\s?] [NC]
RewriteRule ^ /%1 [R=302,L,NE]
</IfModule>
The part I'm struggling with is removing the 'items' part. The rules I found only worked for request path like 'example.com/items/subfolder1/...'
Any help would be greatly appreciated.
To remove /items/ from your URLs you can use the following in your .htaccess file:
RewriteEngine On
RewriteRule ^(.*)/items /$1 [L,R=301]
So for example, this will take the URL: http://example.com/news/items/entry1 and turn it into http://example.com/news/entry1
Make sure you clear your cache before testing this.

Cleaning urls my htaccess

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/

What do these declarations mean in my .htaccess file?

I am a newb to PHP. Can anyone tell me what each line does here. Do I need this? It is giving me errors
RewriteCond %{REQUEST_URI} /~([^/]+)/?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) /~%1/rewrite.php?p=$1&%{QUERY_STRING} [L]
RewriteCond %{REQUEST_URI} /~([^/]+)/?
RewriteRule ^index\.php?(.*)$ /~%1/rewrite.php?p=%1&%{QUERY_STRING} [L]
#there is no ~ character in the URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) ./rewrite.php?p=$1&%{QUERY_STRING} [L]
RewriteRule ^index\.php?(.*)$ ./rewrite.php?p=$1&%{QUERY_STRING} [L]
#WJ-180 fix
RewriteRule ^resume\.php?(.*)$ ./rewrite.php?p=resume\.php$1&%{QUERY_STRING} [L]
If you are new, please read http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html, it is explained very nicely.
p.s. Your title "What do these declarations mean in my PHP .htaccess file?" is incorrect, .htacces is not php file.
These say
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
"Only use the following rule if the request does not match an existing file or directory."
The simple answer: These declarations rewrite every request to a rewrite.php file.
The more comprehensive answer: The RewriteCond and RewriteRule directives are from the Apache module mod_rewrite and provide a rule based URL rewrite mechanism.
It seems that these rules are intended to rewrite every request to a rewrite.php file, either in a specific directory (/~foobar/rewrite.php) or in the root (/rewrite.php).
Those are mod_rewrite configuration entries.
It an Apache mod_rewrite rule. Unless you are using some sort of framework you probably don't need it. See Apache mod_rewrite for more info.