Simplifying the URL using ReWrite Rule - regex

I have a blog which has a number fo categories in a categories.php page. And the original URL looks like this:
http://myname.com/articles.php?category=music
Which I have simplified to:
http://myname.com/articles/music
Using:
RewriteRule ^articles/([\w-]+)/?$ articles.php?cat=$1 [NC,L,QSA]
But is there a way of simplifying this further down to:
http://myname.com/music
--
UPDATE:
I've sorted out the original problem but now I'm hitting another problem. My .htaccess is:
Options -MultiViews
DirectoryIndex articles.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ articles.php?cat=$1 [NC,L,QSA]
RewriteRule ^([\w-]+)/([0-9]+)/?$ articles.php?cat=$1&currentpage=$2 [NC,L,QSA]
RewriteRule ^([0-9]+)/([\w-]+)/?$ article.php?id=$1&title=$2 [NC,L,QSA]
RewriteRule ^articles articles.php
RewriteRule ^categories categories.php
RewriteRule ^about about.php
RewriteRule ^feed feed.php
RewriteRule ^privacy privacy.php
When I visit http://myname.com/articles and http://myname.com/article... everything works fine, but when I go to http://myname.com/categories, http://myname.com/feed, etc I'm redirected back to the home page. Any ideas why this is?

You can use this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ articles.php?cat=$1 [L,QSA]

Related

htaccess rewrite rule with single parameter

When user enters url like
http://example.com/app/abcd123/
I want to show hime page from
http://example.com/app/index.php?param=abcd123
Without changing URL in browser.
I put .htaccess file inside app folder with code
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/index.php [NC]
RewriteCond %{QUERY_STRING} ^param=(.*)
RewriteRule (.*) http://example.com/app/%1? [R=301,L]
You can use this code in /app/.htaccess:
RewriteEngine on
RewriteBase /app/
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /index\.php\?param=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?param=$1 [L,QSA]
Try this .htaccess code. It should help you.
Make sure the rewrite base should be the form the root to your present directory.
RewriteBase /app/
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ index.php?param=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ index.php [QSA,NC,L]
In this, if the user enters like http://example.com/app/qwerty the call will be processed like http://example.com/app/index.php?params=qwerty
This should be working, I tested it. Let me know if you face any troubles.
RewriteEngine On
RewriteBase /app
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php?param=$1 [L]
Check it there: http://htaccess.mwl.be/ or other online htaccess test service

remove folder from url using .htaccess

I know there are plenty of other questions similar to this one, i tried followinf some of those, but with no luck.
I have a website called test.com and i need, when someone seraches for test.com to show the content in test.com/home, without having home in the uerl.
My htaccess file at the moment looks like this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /(.*) home/$1 [L]
RewriteRule /home/(.*) /$1 [L,R=302]
RewriteOptions inherit
RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com$
but when i type test.com it shows a list of folders, not the content inside home.
What am i doing wrong?
thanks
You can simplify your rules to this in root .htaccess
RewriteEngine on
RewriteBase /
RewriteRule ^$ home/ [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?!home/).+)$ home/$1 [L,NC]
Make sure to clear your browser cache before testing this.

how to hide file extension from the middle of the url using htaccess

I have a problem with hide URL in .htaccess. I want to remove .php from the URL.
For Example: convert www.example.com/destination_info.php/Laos-destination-trip to www.example.com/destination_info/Laos-destination-trip
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^./]+)/(.+)$ $1.php/$2 [L]
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^./]+)/(.+)$ $1.php/$2 [L]
To hide .php use following
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
To hide html
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Try this, To remove the .php extension from a PHP file for example yoursite.com/wallpaper.php to yoursite.com/wallpaper you have to add the following code inside the .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
A reference to the full article can be found here
If you want to use the rule in the middle of the URL for an API like in my case, then use the first answer from #anubhava. That worked for me.
Also, I'm using IIS so I had to install the Rewrite Module and then Import the rule from the first answer. Second and Third answers will work only for the last part of the URL.

htaccess to mask a url with multiple rules

I have this htaccess file in the root folder.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{HTTP_HOST} ^localhost/squash_test/find_a_player
#RewriteRule ^(.*) http://localhost/squash_test/home/find_a_player [P]
RewriteRule ^(.*) index.php?route=$1 [L,QSA]
This is working all good.
What i want is, keeping the existing functionality, i would like to mask two urls.
If i visit
http://localhost/squash_test/find_a_player
then it should display the content from
http://localhost/squash_test/home/find_a_player
Similarly i have another url, which needs such masking. I tried to search across the internet and i did get few solutions but none of them correctly.
Any help would be greatly appreciated.
You can use:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(squash_test)/(find_a_player)/?$ /$1/home/$2 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?route=$1 [L,QSA]

Remove parameter from URL

I have URL on my localhost like this:
http://localhost/mysite/index.php?id=8
I want to remove index.php?id= from the URL and show it like this:
http://localhost/mysite/8
Or:
http://localhost/mysite/about
With some character in place of integer 8.
You can use this rule in /mysite/.htaccess:
RewriteEngine On
RewriteBase /mysite/
RewriteCond %{THE_REQUEST} /index\.php\?id=([0-9]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([0-9]+)/?$ index.php?id=$1 [L,QSA]
You're misunderstanding mod-rewrite.
You must use short URLs everywhere in your code (mysite/8), and let your .htaccess redirect these fake URLs to the real one (mysite/index.php?id=8).
You can not expect a .htaccess to beautify your URLs magically.
That being said, the .htaccess you're looking for is :
RewriteEngine On
RewriteBase /mysite
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?id=$1 [L]