Rewrite PHP variables from URL with .htaccess - php

For example I want to change:
www.example.com/forum/thread?id=1&topic=hello
to
www.example.com/forum/thread/1/hello
I've looked around and modified my .htacess file to look like this to modify these URLs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^thread/([0-9]+)/([0-9a-zA-Z_-]+) thread.php?id=$1&topic=$2 [NC,L]
I keep getting a 404 error saying that the file doesn't exist. I'm wondering if it's because I'm removing the .php from the file first using this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
But then when I remove that rule and go to thread.php?id=1&topic=hello it just breaks everything and gives me masses of errors

In your question, you state that you want to change:
www.example.com/forum/thread?id=1&topic=hello to www.example.com/forum/thread/1/hello
However, that is backwards.
The address that you rewrite to needs to be the address that the server can interpret. So, www.example.com/forum/thread/1/hello should be the "friendly" address that the user enters and the rewrite should add the .php extension (although with a rewrite, this will not be visible to the user in the address bar)
Try this:
RewriteEngine on
RewriteRule ^/?thread/([0-9]+)/([0-9a-zA-Z_-]+) /thread.php?id=$1&topic=$2
The reason that you are probably getting errors going to thread.php is that you have a rule to remove the .php (which now the server will not be able to render the page). With the above rewrite rule, you will get to it by going to :
www.something.com/thread/1/hello

Your rule will never get applied because you are using RewriteCond %{REQUEST_FILENAME}\.php -f this condition checks your filesystem to see if /forum/thread/1/hello.php exists and if it doesn't then your rule is ignored. Simply comment out or remove that condition from your rule .
Try :
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^thread/([0-9]+)/([0-9a-zA-Z_-]+) thread.php?id=$1&topic=$2 [NC,L]

Related

How to allow use of dot in rewriting users profile page using htaccess and php

I have encountered a problem in trying to rewrite users profile page with usernames containing a full stop.
First of all, i applied this rewrite rule to my htaccess file;
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_]+)$ profile.php?tg_u=$1 [NC,L]
RewriteRule ^([^.]+)$ $1.php [NC,L]
actually, the rewrite rule worked when accessing users with username containing only letters and username containing a number and underscore(_) but when trying to access users with username such as user.name i do get a 404 page error, i tried changing previous preg_match to this preg_match(a-zA-Z0-9_.) and later to this preg_match(a-zA-Z0-9_.) on my htaccess (rewrite rule) but i noticed that when i want to access my site in localhost, the profile.php page is been shown as index page, i later then use this preg_match(a-zA-Z0-9.) on my htaccess file but still encounter the same problem.
I will be very grateful to know why i encounter this problem and also how to fix it.
You can use:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9_.]+)$ profile.php?tg_u=$1 [NC,L]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^.]+)$ $1.php [NC,L]
-d If the request is not a directory, and -f not a valid file name.
And I added a test not to rewrite to php a page that does not exist.

Remove index.php with the GET superglobal from the URL

I have this code in my htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^(.+)$ index.php/?url=$1 [QSA,L]
And in PHP, I grab the current url request and give it it's controller so if there was http://example.com/index.php?url=about -> this will give me the about controller so it displays the about page (MVC)
Now my question is that how can I remove the index.php?url from every page. For example I want to access the about page then I need it to be -> http://example.com/about
You can see in the htaccess that I am replacing the index.php/?url with whatever I type ($1) But it doesn't seem to work because when I request this url: (example) http://example.com/about I get Object not found! Error 404. And when I have http://example.com I get the controller_home page. However, when i type http://example.com/home it also displays error 404.
If I do this: http://example.com/?url=about or http://example.com/index.php?url=about it works find and gives me the about controller so the ?url isn't getting removed by the htaccess I guess...
Might that be an another error in my php code or what?
Kindly help me as I have been looking over this error about 10 hours and I didn't find why it is behaving like that, my friend has almost the same code and it works perfectly for him.
I am kind of new to php so it might be simple bug...
Thanks in advance for everyone that helps!
If you're using an .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^/?(.+)$ index.php?url=$1 [QSA,L]
If you're editing the main apache config file (httpd.conf or apache2.conf). I know the OP is using .htaccess but just in case other people having this issue see this discussion:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-l
RewriteRule ^/?(.+)$ index.php?url=$1 [QSA,L]
First, presumably you want to test if the requested string does NOT match a directory, NOT match a file, and NOT match a symbolic link - so you need add a "!" before the RewriteCond flags (to negate the condition). Stacked RewriteConds are linked with an implicit AND so checking for directory, file and symbolic link doesn't make sense (a given requested string wouldn't match all of these conditions).
Second, the %{DOCUMENT_ROOT} would be necessary if you're doing this in the main apache config file (ie/httpd.conf or apache2.conf). It's not necessary if you're doing this in an .htaccess file.
Third, when RewriteRule is matching it includes the /. Presumably when you request "website.com/about" you want "about" to be passed to index.php not "/about", so the "/?" will remove the / in first character position from $1. I put the question mark, just because I think it's best practice, since technically you could get an HTTP request that does not begin with a slash (although this would be against standards and every browser includes a slash at the beginning).
Fourth, you shouldn't put a / after the index.php in RewriteRule (so it should be "index.php?url=$1", not "index.php/?url=$1". Query strings are separated from a filename via a "?" not a "/?".
Try this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^(.*)$ http://%1/$1/ [R=301,L]
RewriteRule ^((.*)+)$ index.php?url=$1 [QSA,L]

RewriteRule to redirect to php file not working

I just want a simple redirect to clean up the url's on a site.
e.g.
I want ajhtestserver.com/registration/ to redirect to ajhtestserver.com/registration.php
It should be easy and I have successfully used .htaccess rewrites on other sites but for some reason it just will not work for me today.
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^registration[/]$ registration.php [NC,L] # Handle requests for "registration"
I am sure it is something simple that I am missing but I basically just copied what I have on other sites that work fine for me so I am confused as to why it just refuses to work for me here (gives me The requested URL /ajhtestserver/registration/ was not found on this server. error). Just one of those days :(
Any help is appreciated.
Thanks,
Adam
if you use apache ,first you should enable rewrite_mode in http.conf or ...\
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^registration/(.*)$ registration.php/$1 [L]
check .htaccess syntax or rewrite mode.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)[/]$ $1.php [L]
Well it didn't seem to like it when the redirect source word and target filename were the same word but this works...
RewriteRule ^([a-zA-Z\ ]+)[/]?$ $1.php [NC,L]
And that is actually a better solution anyway as it doesn't require a separate rule for each page.
Though I never did figure out why it didn't like it the original way.

Removing .php extension in localhost

I've been trying to figure out how to remove .php extension, I've searched everywhere and it seems most of the code are not working anymore. The code below is what I am using now to remove the .php extension but it is not working.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !- f
RewriteRule ^([^\.]+)$ $1.php [NC]
I'm using XAMPP on Windows 10. I also tried many times to remove the .php extension but the result was the same. But after a long while finally, I got how we can do this. If you want the same results, just follow the steps given below...
To remove the .php extension from a PHP file, e.g. if you want to change mywebsite.com/about.php to mywebsite.com/about you have to add the following code inside your .htaccess file:
First of all, make sure you've created a new file named .htaccess and placed it at your root-level/root-directory where your index file is placed.
Now, open this (.htaccess) file with any editor of your choice, copy/paste the given code and save it...
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [NC,L]
Now, if you (any user) will access mywebsite.com/about in the browser, the user will see the content of mywebsite.com/about.php page.
But still, if you (any user) will access the URL as mywebsite.com/about.php, this will not redirect the user to this mywebsite.com/about page. But will go to this mywebsite.com/about.php page which means the user can still visit mywebsite.com/about.php page. Don't worry about it. If you want to avoid this, you can simply follow the next step.
To avoid the above problem, now you need to add some more rules in the .htaccess file. For this, you've to replace your old code with this new one given below, save your file and check it out...
RewriteEngine On
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
That's all and you're all set.
I hope this will fix everyone's problem.
Have a nice day :)
To remove the .php extension from a PHP file
for example yoursite.com/wallpaper.php to yoursite.com/wallpaper you have to add the following code inside the .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Or
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L, QSA]
Or
see removing .php extension from URL
Also, make sure that you've mod_rewrite on.
Also see how to create .htacess file
I think you need to change internal website coding as well with htaccess code above and remove the file name URL extension from where it is mentioned in your web coding.
Eg:-
https://yoursite.com/page.html
changed to
https://yoursite.com/page/
htaccess code
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]

Using htaccess to remove .php and allow path

I currently have a .htaccess file that allows people to enter the URL without the php extension, such that http://domain.com/account redirects to account.php
I would like to be able to have it so that if I enter http://domain.com/account/contactinfo (or http://domain.com/account/settings/groups and so on) it still goes to account.php, but I am not sure how to change what I have to achieve this.
Current .htaccess :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(([A-Za-z0-9\-_\.]+/)*[A-Za-z0-9\-_\.]+)?$ $1.php
Any help appreciated! Obviously if there exists a folder it should follow that path (e.g. if /folder/page.php exists, then http://domain.com/folder/page/create would go to folder/page.php)
Try this is you don't need to pass any URI info into query string (i.e. your app will still look at $_SERVER['REQUEST_URI'])
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9\-_\.]+)(/[A-Za-z0-9\-_\.]*)?$ $1.php&q=$2 [QSA]
# Note the optional '&q=$2' on line above if you want to make removed part of URI available as passed parameter
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ / [L,QSA]
Note that since I removed the condition to check for a valid php file, I added a second conditional rewrite rule to just redirect to site root if the re-written request does not point to a valid PHP file. You could obviously redirect this to a 404 page or whatever else you might want to redirect to. Or you could remove this altogether and let Apache give it's default 404 response.

Categories