htaccess mod_rewrite rules combination issue - php

I'm so newbie in apache, so it's a basic question but I couldn't solve this using another questions or links.
I tried to change my URL using .htaccess and I had two purposes;
hide .php extension
change some queryString from somefile.php?id=bar to somefile/id/bar which bar is a number or mixed string like T51-3.
My queryString is http://localhost/payment/theme/ticket?id=770314 that I want change it to http://localhost/payment/theme/ticket/id/770314 or
http://localhost/payment/theme/ticket/770314
I found this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]
in stackoverflow.com/this_question
This was working nice but didn't solve my second issue.
So, I searched for a while in this site and others and did find some sample codes like this one :
RewriteRule ^([a-zA-Z0-9_-]+)-([0-9]+)$ ticket.php?id=$2
or
RewriteRule ^(.*)$ /ticket.php?url=$1 [L]
and etc...
but none of these didn't work for me.
Some of them will make the server done and some don't have any affect at all.
Would you please guide me to right way...
I looked at these questions:
htaccess rewrite for query string
how to change query string parameters with name using .htaccess?
How can I use .htaccess to hide .php URL extensions?
And these links:
http://simonecarletti.com/blog/2009/01/apache-query-string-redirects/
https://wiki.apache.org/httpd/RewriteQueryString
UPDATE :
This is my site strucure:
There is an htaccess for important folders, like, forms, db, classes etc...
All my site pages are in the theme folder, as you can see in the picture.
In the root htaccess, there's just two lines that auto start sessions.
And I added your code to theme's htaccess.
Thanks in Advance

You can use this code in
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /payment/theme/
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php\?(\w+)=([^\s&]+)\s [NC]
RewriteRule ^ %1/%2/%3? [R,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# to handle http://localhost/payment/theme/ticket/id/770314
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\w+)/([\w-]+)/([\w-]+)/?$ $1.php?$2=$3 [L,QSA]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

Related

How to use parameters when file php extension is removed?

Instead of
http://example.com/file.php?color=red
I would like to use
http://example.com/file/red
I have removed my file extensions by adding the code below to .htaccess which will display the content of the http://example.com/file.php on the server when accessing http://example.com/file/
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
Now I am looking for a way to pass the "red" parameter to the file when putting this address in the web browser like this http://example.com/file/red
Thanks!
EDIT:
I got the answer to look for something called friendly URLs and that helped me.
I ended up with replacing the first code with this.
Options -MultiViews
RewriteEngine On
# redirect "/file.php?id=xxx" to "/file/xxx"
RewriteCond %{THE_REQUEST} \s/file\.php\?color=([A-Za-z0-9_]+)\s [NC]
RewriteRule ^ /file/%1? [R=301,L]
# internally rewrite "/file/xxx" to "/file.php?id=xxx"
RewriteRule ^file/([A-Za-z0-9_]+)$ file.php?color=$1 [L]
Then with PHP I will just request the parameter like this.
$color = $_GET['color'];

convert dynamic url to seo friendly url in htaccess

Cuurent URL is
https://www.website.com/colleges.php?id=NR%20School%20of%20Architecture
we want to change this URL into SEO friendly URL as like,
https://www.website.com/colleges/NR-School-of-Architecture
we used this htaccess,but only the spaces were removed
RewriteEngine On
RewriteBase /
# replace all space by hyphen
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*?)(?:\+|%20|\s)+(.+?)\sHTTP [NC]
RewriteRule ^ %1-%2 [L,NE,R=302]
we used this htaccess to remove .php, it worked and .php was removed in the entire site.
But we dint get a seo freindly url like this,
colleges/NR-School-of-Architecture
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
kindly help us in achieving seo friendly url using htaccess
Thanks in advance.
You will need to define the rewrite rules in a .htaccess file placed in your site document root location after you have confirmed that mod-rewrite is enabled
RewriteEngine On
RewriteBase /
RewriteRule ^home /index.php

Hide extension of php files and hide index.php with htaccess

This has been asked a thousand times, but not exactly how I want it. I have tried combining different solutions, but my .htaccess doesn't seem to do what it is supposed to.
# Not sure what this does?
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
# Redirect index in any directory to root of that directory
RewriteCond %{THE_REQUEST} ^[A-Z](3,9)\ /([^/]+/)*index\.[^\ ]*\ HTTP/
RewriteRule ^(([^/]+/)*)index(\.[a-z0-9]+)?$ http://www.example.com/$1? [R=301,L]
Now my pages correctly change from domain.com/page1.php to domain.com/page1, however something goes wrong with domain.com/index.php.
I am testing this locally and when going to localhost/project everything works fine (the index.php opens, but you don't see that in the url) but when you explicitly navigate to localhost/project/index.php you are returned to the very root, i.e. localhost (which hen returns to http://localhost/xampp/splash.php). Of course, this is not what I want. I want localhost/project/index.php to return to `localhost/project/ยด.
An additional question, though: how do rewrite rules influence search engines. Will the pages (ie contact.php, about-us.php and so on) still be indexed?
Extra +1 and kudos for he or she who gives a detailed breakdown of what each line/rule in the htaccess in their answer does. I am still learning .htaccess, so every detail is important and relevant to me!
Lot of comments in this code in question seem to be mine :)
Anyway you can use this code to fix your issue:
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /project/
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} \s/+project/+(.*?/)?(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ %1%2 [R=302,L,NE]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/project/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
Update: For using in DocumentRoot:
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ %1%2 [R=302,L,NE]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

url rewrite to short url

Good days guys, I need your help with this url rewrite.
The are two rewrites I want to do.
1) I want to take out .php from the url even though my files is saved as .php
2) I want to rewrite the url below
If you can help me. It will be lovely. Thanks
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your DOCUMENT_ROOT/.htaccess file:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php[\s?] [NC]
RewriteRule ^ %1 [R=301,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]

hiding .php address bar object not found

I have a simple problem, i tried to remove .php on the address bar using this code below
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# To externally redirect /dir/foo.php to /dir/foo/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally redirect /dir/foo/ to /dir/foo.php
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
and it works but eventually it keeps on displaying "OBJECT NOT FOUND" how do i fix this problem?
You may try to use this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Example: Your page is: www.yoursite.com/yourpage.php . Now you can access the same page without ".php".
ALSO
You can try to remove [R] permanent redirect (to remove your error), and mantain the extension in rule.
I hope this will help you!
If all you want to do is remove the .php extension from your URLs, just use Options +MultiViews, no need for the rewrites.

Categories