htaccess to mask a url with multiple rules - regex

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]

Related

Simplifying the URL using ReWrite Rule

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]

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.

Htaccess rewrite Single, or multiple $_GET parameter on LINUX base server

Below's url is I need .htaccess rewrite from ?view=home to /view/home
http://example.com/?view=home
And what if my url is quite dynamic with $_GET Parameter that have multiple $_GET?
http://example.com/?view=home&url=this&name=test&id=that
I had try few rewrite module but cant get result that I wanted.
Below is my rewrite engine :
Options +FollowSymlinks
RewriteEngine On
# specific rule
RewriteCond %{QUERY_STRING} ^([^=]+)=([^&]+)
RewriteRule ^(constant)$ /index.php?view$1 [L]
# general catch-all
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L]
My current web hosting is on Linux Web Hosting, locally running my tested file is on XAMPP environment.
You can this code to redirect from ?view=home to /view/home:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{THE_REQUEST} /\?([^=]+)=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?$1=$2 [L,QSA]
# specific rule
RewriteCond %{QUERY_STRING} ^([^=]+)=([^&]+)
RewriteRule ^(constant)$ /index.php?view$1 [L]
# general catch-all
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L]

htaccess: redirect 301 doesn't work properly

This code is working fine:
RewriteEngine On
RewriteBase /my/project/dir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /my/project/dir/index.php?uri=$1 [QSA,L]
But now I'd like to force the www. within the URL to avoid duplicate content (SEO). I found this code-snippet, but I'm not able to integrate it into my code:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.com$
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
I tried a few combinations but I don't want to confuse you. None of them was working.
How would you extend the first (working) code lines to accomplish the goal? Thanks in advance!
Change the order of rules and use %{REQUEST_URI} in 301 rule:
RewriteEngine On
RewriteBase /my/project/dir/
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^ http://www.domain.com%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /my/project/dir/index.php?uri=$1 [QSA,L]

ajax file not loading because of trailing slash

I have an ajax file that is called when someone begins to type in search bar. I have recently been cleaning up my urls and removing file extentions adding trailing slashes, since then my ajax file doesnt appear to load anymore. can anyone help? here my htaccess so far
Options +FollowSymlinks
Options +Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^([^.]+)\.php$ /$1 [R=301,L]
Assuming that your AJAX requests go to the /includes folder, and your normal pages do not, we can modify your rules a bit so that they look like this (including Cags' comment about the RewriteCond):
Options +FollowSymlinks
Options +Indexes
RewriteEngine on
# We'll do the redirect first, so no other rules get in the way
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^.#?\ ]+)\.php([#?][^\ ]*)?\ HTTP/
# Make sure the request didn't start with "includes"
RewriteCond %1 !^includes/
RewriteRule ^([^.]+)\.php$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
I also think that you either wanted /? at the end of the rules in the center block, or /$1/ as the replacement on the rule in the first block, so that the redirect from /page.php to /page gets interpreted correctly after the first redirect (I think right now you'll get redirected twice, once by the first block, and again by the last block).