Url rewrite to takeout - php

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
www.example/account/product.php?Sef=shoes&pid=3
To something like this
www.example/account/product.php?Sef=shoes/pid/3
If you can help me. It will be lovely. Thanks

For your second problem you don't need htaccess for that just parse $_GET['Sef'] on your server side.
As per php extension try this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Related

.htaccess Rewrite - Masking Dynamic .php extensions

I understand that similar questions have been asked before in regard to .htaccess, but after hours of reading through the Questions, Answers and Comments, as well as countless hours reading .htaccess documentation and even trying .htaccess generators... and messing my site access up via trying examples... I come to seek those wiser than me for guidance.
I am using the following .htaccess file to remove .php extensions from URLS shown to the user in the browser
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
This works perfectly
www.example.com/users/player.php?id=1 is being correctly shown as
www.example.com/users/player?id=1
I am now struggling to write/find/understand examples that will allow me to rewrite the full extension
I want to showwww.example.com/users/player.php?id=1aswww.example.com/users/1
totally removing the player.php?id= part of the dynamic URL
Thank you in advance for any help/guidance.
For anyone else who wants to do what I wanted to do, I have found a solution.
Question: Htaccess rewrite with id
Answer: from Anubhava
This will allow you to externally mask "edit.php?id=1" to "edit/id/1" as well as use either URL interchangeably.
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/([^/]+)/edit\.php\?id=([^\s&]+) [NC]
RewriteRule ^ %1/edit/id/%2? [R=302,L,NE]
RewriteRule ^([^/]+)/edit/id/([^/.]+)$ $1/edit.php?id=$2 [NC,L,QSA]
Best of luck!

URL rewriting PHP losing querystring

I have a dynamic URL like
<domainname>/sub/cl/<phpfile.php>?c=123
phpfile.php can be any php file which comes in run time
/sub/cl/ is a folder path.
i have use this code in .htaccess:
RewriteRule ^sub/cl/new-file/([0-9]+)/?$ sub/cl/newfile.php?c=$1 [NC,L]
this is correctly redirecting to the newfile.php when i hit /sub/cl/new-file/321/ but sub/cl/ get appended before all css and js which are in root and also c=321 get lost
Please help me what m doing wrong.
Thanks in advance
if your problem is only query string insert [QSA] flag to to the rule. JS/CSS/IMG files are losing because the rewrite works for every request. Over come this issue by adding RewriteCond
RewriteCond %{REQUEST_FILENAME} !-f #Files
RewriteCond %{REQUEST_FILENAME} !-d #Directories
further reading
Apache Mod_rewrite

Remove file extension on url

Good day!
Got a lot of questions here but i guess i can't ask it all at once so i'll start with the problem i am facing now.
So, I am new to .htaccess and I don't know how or if it is possible to remove the file extensions on url, like for example I am accessing my home page in this url "www.sniper.com/home.php", so the question is clear. Is it really possible to use .htaccess to remove the file extension so that when you are at homepage the url will look just like "www.sniper.com/home" ?
To remove .php extension
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Mod rewrite and dynamic URLs

i know this is a common question but i cannot figure this one out.
I have the following URL:
http://buildsanctuary.com/viewbuild.php?id=3&title=this_is_a_title&page=1
What i wish is the URL to be:
http://buildsanctuary.com/viewbuild/3/this_is_a_title/1
Normally for mod rewrites i would send the user to the link i want and let htaccess do all the work.
So in this case i have tried linking the users to the preferred URL style and rewriting the URL but to no avail.
Any help on how i should be handling this? I want to send the users to the preffered URL but then can i use htaccess to allow me to process the page and URL $_GET information in the same way as the normal dynamic URL?
I have tried the mod rewrite generators etc but nothing works.
This is what the gens have given me:
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.php$ /viewbuild.php?id=$1&title=$2&page=$3 [L]
Thanks.
Fix the generator rule:
RewriteRule ^viewbuild/([^/]*)/([^/]*)/([^/]*)\.php$ /viewbuild.php?id=$1&title=$2&page=$3 [L,QSA]
However, I would do it this way:
RewriteRule ^([^/]*)/(.*) /$1.php/$2 [L]
and then map what you get in $_SERVER['PATH_INFO'] to you preference in PHP.
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?id=$2&title=$3&page=$4 [L,QSA]
Should do the trick
^viewbuild/([0-9]+)/([a-z_]+)/-([0-9]+)/?$ viewbuild.php?id=$1&title=$2&page=$3 [NC,L]

Use .htaccess to hide .php and query name

I have been searching all over google and stackoverflow, and have been finding a large number of answers but none seem to work for me.
Basically what I want is to rewrite my url localhost/index.php?page=1 to localhost/1, while still keeping the query alive.
Now, I have managed to remove the .php with the following code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
But removing the Query simply does not seem to work.
Please help me out.
Your rule doesn't match what you're trying to do. You're telling it to take 1 or more of anything and make it into blah.php (like http://localhost/blah -> http://localhost/blah.php).
You're looking for something like RewriteRule ^([0-9]+)$ index.php?page=$1 [NC,L].
By the way, your language is a bit backwards. You're not rewriting index.php?page=1 to localhost/1; you're rewriting localhost/1 to index.php?page=1 (well, really 1 to index.php?page=1).

Categories