Issue with .htaccess for friendly urls - php

This is my currently my .htaccess file
Options -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC]
RewriteRule ^(.*)/edit/(\d+)?$ $1/edit.php?tag=$2 [NC]
As you can see it's set to hide the .php extension, but this then seems to break the edit rule. If I comment out
RewriteRule ^(.*)$ $1.php [NC]
The edit rule works fine, but I need both and cannot seem to get it to work, anyone see what the problem is and how to sort it.
[Edit]
I have a link like this and when all rules are active this is what doesn't work.
http://www.domainname.com/researcher/lists/edit/
and I get an 500 Internal Server Error.

Try rules as in your site root .htaccess:
Options -MultiViews
RewriteEngine on
RewriteRule ^(.+)/edit(?:/(\d+))?/?$ $1/edit.php?tag=$2 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Related

Change url from profile.php?user=myUsername to profile/myUsername with .htaccess

I am using xamp to host my webpage on my PC and I want to change this url from
http://localhost/loginsystem/profile.php?user=myUsername
to
http://localhost/loginsystem/profile/myUsername
I have tried the following and doesn't work and in return gives server error 500.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/profile/([a-zA-Z0-9]+) /profile.php?user=$1 [NC, L]
and
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/profile/(.*)?$ /profile.php?user=$1 [NC, L]
Any help would be great still pretty new. Thanks in advance!
This should do the trick
RewriteRule ^profile/([^/]+)/?$ profile.php?user=$1 [L]
Also make sure that your .htaccess file is in the loginsystem folder. Otherwise you will need to put this in your rewrite rule
RewriteRule ^loginsystem/profile/([^/]+)/?$ profile.php?user=$1 [L]
Replace .htaccess code with:
Options +FollowSymLinks
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^profile/([a-zA-Z0-9]+)/$ profile.php?user=$1 [L]
Now you can get user query with GET request.

url rewriting not working on one.com provider

Currently i am trying to get url rewriting to work on one.com hosting provider which is a pain and support don't understand the issue, so i was thinking of asking here if you may assist me finding the issue.
I'm using this rewriterule which worked on localhost but not with the provider:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?key=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?key=$1
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?key=$1&seo=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ index.php?key=$1&seo=$2
</IfModule>
RewriteCond %{HTTP_HOST} ^mysite\.com
RewriteRule ^(.*)$ http://www\.mysite.com/$1 [R=permanent,L]
and in index:
if($key=='post') //Cant include $seo here because the variable differ for
//each post on the site
{
include('post.php'); // Post page
}
Then in post.php ofc i take the seo GET variable to display the post content on the page.
complete url www.mysite.com/post/test-test-test
The error i end up with is 404
Not Found
The requested URL /post/test-test-test.php was not found on this server.
So basically what i understand is that it tries to enter a folder named /post/ and look for the file test-test-test.php
So i believe it doesn't include the rewrite rule, or can you find an error in the rewrite rule which worked on localhost?
You need to rearrange your rules and for adding .php extension make sure a file with .php exists:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L,NE]
# ignore rest of the rules for files and directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([\w-]+)/?$ index.php?key=$1 [L,QSA]
RewriteRule ^([\w-]+)/([\w-]+)/?$ index.php?key=$1&seo=$2 [L,QSA]
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
</IfModule>

htaccess rewrite url to hide php question mark

my url formatmysite/profile/?theusernamewith hide php extension, im trying to hide question mark in url so the url will like mysite/profile/theusername, looked up few posts about what i should do is add external redirect then internal forward in htaccess, tried lot of code still can't get it work. this is what i have now:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
Keep your .htaccess like this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /profile/
# new rules
RewriteCond %{THE_REQUEST} \s/+profile/?(?:index\.php)?\?([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/?$ index.php?$1 [L,QSA]
# php hiding
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Remove .PHP and redirect through htaccess

Is that possible to hide the .PHP extension and redirect the URL at same time.
example : http://example.com/test need to redirect to http://someothersite.com
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^(.*)/test http://someothersite.com
But its not working.
Any idea ?
Thanks
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^test http://someothersite.com [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
The code above is not tested, but it should give you an idea about it. Use L flag after each set of rules, as it stops processing of the rest of the rules.
You need just this:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ $1.php [R=301,L]
For cross domain rewriting:
RewriteRule ^test http://someothersite.com [R=301,L]
Allways use L = 301 for permanent redirectings.

htaccess prevent page extensions displaying

I have my htaccess file set up so that my website pages don't need to display an extension, my links are setup like this, with the slash at the end:
www.mywebsite/about/
These links work - But if I were to directly type in
www.mywebsite/about.html
The website will show this page with the html extension. Is there a way that I can prevent extensions from showing even if they are directly typed in? :S
The majority of my pages are html, except for one php page. This is what I have in my htaccess:
Options -Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)/$ $1.html [L]
RewriteRule ^php-page/([^/\.]+)/?$ php-page.php?p=$1 [L]
If anyone can help me out with this I would really appreciate it! :)
Please try this:
Options -Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^(.*)\.html$
RewriteRule .* %1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)/$ $1.html [L]
RewriteRule ^php-page/([^/\.]+)/?$ php-page.php?p=$1 [L]
I think it can be done a bit better than this, but it should work.

Categories