.htaccess: Slugs and file extensions - php

I'm looking for a way to disable access to file, if not accessed through slug. for example, I have /members.php?page=login. I want to change it, by forcing the user access /login (redirect the long url into a short slug) and prevent direct access from the long url.
When I try to do something like:
RewriteRule ^members.php?page=login login [L,R=301]
RewriteRule ^login members.php?page=login [L]
It turns into an infinite loop (as firefox says: "Firefox has detected that the server is redirecting the request for this address in a way that will never complete.").
I've enabled the "options" at the beginning of file (Options +FollowSymLinks -MultiViews)
How can I make it right?
Thanks.

You can use the following :
RewriteEngine on
# externally redirect "/members.php?page=login" to "/login"
RewriteCond %{THE_REQUEST} /members\.php\?page=login [NC]
RewriteRule ^ /login [L,R]
#rewrite "/login" back to "/members.php?page=login"
RewriteRule ^login members.php?page=login [L]

try this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^login$ index.php?page=login [L]

Related

url rewrite using htaccess without redirection

I have a url like this:
http://www.localhost.com/code_category/computers/
I want to change this url to:
http://www.localhost.com/category/computers/
I don't need url redirection.
My current htaccess file looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
You only want to redirect code_category to categoryexternally and keep the path as it is internally so, try this :
RewriteCond %{THE_REQUEST} !\s/+category/ [NC]
RewriteRule ^code_category/(.*)$ category/$1 [R=302,L,NE]
RewriteRule ^category/(.*)$ code_category/$1 [L]
The above will redirect any request containscode_category/whatever to category/whatever externally and keep the internal path as it is .
If you want only request contains code_category/computers/ change it to this :
RewriteCond %{THE_REQUEST} !\s/+category/computers/ [NC]
RewriteRule ^code_category/computers/(.*)$ category/computers/$1 [R=302,L,NE]
RewriteRule ^category/computers/(.*)$ code_category/computers/$1 [L]
test it , if it is fine change 302 to 301 for permanent redirection.
Note: clear your browser cache then test it.
.htaccess file
Add this code
RewriteEngine on
RewriteCond %{HTTP_HOST} ^localhost.com [NC,OR]
# without redirect
# RewriteRule ^/code_category/computers/$ category/computers/
RewriteRule ^/category/computers/$ code_category/computers/
# redirect method
# RedirectMatch 301 ^/code_category/computers/$ category/computers/
RewriteEngine On enables mod_rewrite.
RewriteCond %{HTTP_HOST} shows which URLs we do and don't want to run through the rewrite.
In this case, we want to match example.com.
! means "not." We don't want to rewrite a URL that already includes folder1, because then it would keep getting folder1 added, and it would become an infinitely long URL.
[NC] matches both upper- and lower-case versions of the URL.
RewriteRule defines a particular rule.
The first string of characters after RewriteRule defines what the original URL looks like. There's a more detailed explanation of the special characters at the end of this article.
The second string after RewriteRule defines the new URL. This is in relation to the document root (html) directory. / means the html directory itself, and subfolders can also be specified.
For Reference click here
Hope this helps!

htaccess make different page acting as home page

If user goes to URL
http://example.com
The server does a 302 Moved and goes to http://example.com/en/home/my-page.html
What I want is if URL is
http://example.com/en/home/my-page.html
The browser should just show http://example.com/en/
I've tried with .htaccess like so:
RewriteEngine on
RewriteCond %{THE_REQUEST} ^en/home/my-page\.html
RewriteRule ^en/ /en/home/my-page\.html [R=301,L]
RewriteRule ^index.php$ en/home/my-page\.html [L,R=301]
but it does nothing. What am I doing wrong?
Your regex patterns are wrong.
You can use these rule in site root .htaccess:
RewriteEngine on
# externally redirect /en/home/my-page.html to /en/
RewriteCond %{THE_REQUEST} \s/+en/home/my-page\.html[?\s/]
RewriteRule ^ /en/ [R=301,L]
# internally rewrite /en/ to /en/home/my-page.html
RewriteRule ^en/?$ en/home/my-page\.html [L,NC]
Don't forget to clear your browser cache before testing this change.
This code will redirect user hitting
http://example.com/en/my-page.html
to
http://example.com/en/
and than will internally redirect thr request to index.php (you can substitute parameter with the directory where index.php is located or just remove it if it is in the main dir)
RewriteRule ^en/my-page\.html$ /en/ [L,R=301]
RewriteRule ^en/ /<dir>/index.php [L]

Mod Rewrite ignore url

I am creating a site where instead of using GET variables I will just be looking at the url. How would you create a mod_rewrite rule that no matter what directs the user to index.php(Or some page)?
EX:
User enters: www.example.com/blog/programming/postName
User still sees www.example.com/blog/programming/postName in the adress bar but www.example.com/index.php is shown
I have tried:
RewriteRule ^([a-z]+.)?$ /index.php [NC,L]
But that only changes the page for one directory(only worked for www.example.com/worksNow/
RewriteRule ^(.*) /Blog3/index.php [NC,L]
But It got a server error
This behavior/recipe doesn't require mod_rewrite, see FallBackResource.
http://httpd.apache.org/docs/2.2/mod/mod_dir.html#fallbackresource
You can use this rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php [L]
This means if any request that is not for a file or directory then internally rewrite that to /index.php
Reference: Apache mod_rewrite Introduction

.htaccess rewrite to produce clean url

Currently I set a rewrite rule like so, to produce clean and simple url's.
.htaccess
RewriteRule ^/about$ about.php [L]
But what i need to do is something a little different, the other way around. For example if a user clicks the following link
about
They would go to the about page /about
Currently the about page resides at index.php?a=about&b=user and this can't be changed unfortunately. As you can see it does not look very nice in the browser address bar.
EDITED
I am using a pre-made script from phpdolphin, which is working fine. But the url are all index.php based and i would like to clean them up
Currently the only code within the .htaccess file
RewriteEngine on
RewriteCond %{request_filename} -f
RewriteRule ^(.*) $1 [L]
RewriteRule ^(([^/]*)+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L]
Add this rule before your existing rule:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?a=([^&]+)&b=user[\s&] [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteRule ^([^/.]+)/?$ index.php?a=$1&b=user [L,NC,QSA]
You can add this RewriteRule to redirect the request when user hit index.php?a=about&b=user
RewriteCond %{QUERY_STRING} ^(.*)a=about(.*)$ [NC]
RewriteRule ^index.php /about [L,NC,R=301]
or you can use php header() function in index.php to redirect the request:
if ($_REQUEST['a'] == about) {
header('Location: /about');
exit;
}
Try this i checked and it works ...
RewriteEngine On
RewriteRule ^([A-Za-z0-9-+_%*?]+)/([A-Za-z0-9-+_%*?]+)/?$ index.php?a=$1&q=$2 [L]
Note: add additional signs between [] if necessary
OR This
RewriteRule ^([^~]+)/([^~]+)/?$ index.php?a=$1&q=$2
I like to use this code because it's short and matches everything except for ~ which is very very rare to see in a url

mod_rewrite to remove .php but still serve the .php file?

I just wanted to do a simple thing with mod_rewrite. I have a site which uses .php files, and I wanted to rewrite those to cleaner URLs, and remove the .php. So, files would be www.mysite.com/contact and so on.
This does work how I wanted, but I had expected that it would still serve my contact.php file, but just show the user that they were at /contact rather than contact.php. But, it is looking for a file just called contact, which, is not there.
So, what so I need to do, do still use my contact.php file, but rewrite the URL for the user to /contact ?
Here is what I am using:
SetEnv APPLICATION_ENV development
RewriteEngine on
RewriteBase /
# Always use www.
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
# Change urlpath.php to urlpath
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
RewriteRule ^(.*)\.php$ http://www.mysite.com/$1 [L,R=301]
For this solution, I have followed the following rules:
If the user tries to load /something.php they should be externally redirected to /something.
If the user tries to load /something then they should be internally redirected to /something.php.
If the user passed any query string parameters to the URL then these should be preserved through the redirects.
If the user tries to load a different file which really exists on the filesystem (a stylesheet, image etc) then this should be loaded as is.
And here's the final set of mod_rewrite magic:
RewriteEngine on
RewriteBase /
## Always use www.
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
# Change urlpath.php to urlpath
## Only perform this rule if we're on the expected domain
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
## Don't perform this rule if we've already been redirected internally
RewriteCond %{QUERY_STRING} !internal=1 [NC]
## Redirect the user externally to the non PHP URL
RewriteRule ^(.*)\.php$ $1 [L,R=301]
# if the user requests /something we need to serve the php version if it exists
## Only perform this rule if we're on the expected domain
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
## Perform this rule only if a file with this name does not exist
RewriteCond %{REQUEST_FILENAME} !-f
## Perform this rule if the requested file doesn't end with '.php'
RewriteCond %{REQUEST_FILENAME} !\.php$ [NC]
## Only perform this rule if we're not requesting the index page
RewriteCond %{REQUEST_URI} !^/$
## Finally, rewrite the URL internally, passing through the user's query string
## using the [qsa] flag along with an 'internal=1' identifier so that our first
## RewriteRule knows we've already redirected once.
RewriteRule ^(.*)$ $1.php?internal=1 [L, QSA]
Your third rule should be the other way around:
# Change urlpath.php to urlpath
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !\.php$ [NC]
RewriteRule ^/?(.*)$ $1.php [L,R=301]
Once the user goes to /contact, it'll load contact.php. The extra RewriteCond is so that if people DO go to contact.php, it won't try to load contact.php.php
As I understand you want the URL to be /contact even if the URL was /contact.php.
You can check for the .php extension and do a redirect to remove it. Use R=301 (as you do).
Then you have to make your server accept the URL without the .php extension. It might actually already do that.
That's what mod_negotiation does. It should be installed by default, but you might have to enable it.
You can also do that with mod_rewrite, but remove the R from the options. It will redirect internally instead of answering with an HTTP redirect.

Categories