PHP Clean URLs not working as intended - php

I am trying to use a .htaccess file to change my URLs from
xxx/table/change.php?id=1
to
xxx/table/change/1
I have the following code in my .htaccess file, which is in the root folder of my web site.
RewriteEngine On
RewriteRule ^change/([^/\.]+)/?$ change.php?id=$1 [L]
RewriteRule ^change/([^/\.]+)/?$ change.php?id=$1 [L]
However when I restart Apache and visit the desired URL, it still shows up as
xxx/table/change.php?id=1
Any suggestions?
mod_rewrite is uncommented in the httpd.conf file.

how about you use
change/(\d+)$ change.php?id=$1 [L]
that is when you visit
xxx/table/change/1
you are not redirected or 404, you get the correct page. is that what you are looking for?

Related

I have problem in .htaccess in my localhost not redirecting correctly

My .htaccess file contains:
Options +FollowSymlinks
RewriteEngine On
RewriteRule projects$ projects.php
RewriteRule projects/$ projects.php
RewriteRule projects/page/([^/]*)$ projects.php?page=$1
RewriteRule projects/page/([^/]*)/$ projects.php?page=$1
RewriteRule projects/company/([^/]*)/$ projects.php?companyid=$1
RewriteRule projects/company/([^/]*)$ projects.php?companyid=$1
RewriteRule projects/([^/]*)$ projects.php?id=$1 [L]
It was working fine, but suddenly this section is not working in my website.
This should redirect to "projects.php?companyid=$firstvariable",
but actually, I don't know why.
I tried restarting my PC and deleting temp and restarting WAMP.
After some interactive troubleshooting with OP, it appears that WAMP's Apache/PHP will execute a PHP file that matches the directory portion of the URL path if it exists, before applying the .htaccess RewriteRules.
In other words, if you have a file named Abc.php, and you request /Abc/def, WAMP's Apache/PHP will just execute Abc.php and not consult the .htaccess file.
I'm not sure if this is something special about WAMP and whether it's out-of-the-box behavior. I can't find any documentation on being able to control/change this behavior.

.htaccess mod rewrite and MAMP

having issues with what i think is linked to the mod rewrite in mamp.
The rewrite works fine on the live server, but im working locally with MAMP and getting issues.
The problem is linked to this:
I rewrite the following URL:
http://localhost/BuildSanctuary-Dev/viewbuild/64/three-fiddy-z/1
That should rewrite as:
http://localhost/BuildSanctuary-Dev/viewbuild.php?id=64&title=three-fiddy-z&page=1
The issue is that i get a 404 for page not found.
The requested URL /viewbuild.php was not found on this server.
Which it clearly is... but if i visit just /viewbuild.php it works fine.
The rewrite in the .htaccess is:
RewriteRule ^viewbuild/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ /viewbuild.php?id=$1&title=$2&page=$3 [L,QSA]
Any ideas? I have done the research and everything in the apache conf is showing as Allowing All...
Thanks.
Your rewriterule redirects to /viewbuild.php and not /BuildSanctuary-Dev/viewbuild.php:
RewriteRule ^viewbuild/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ /BuildSanctuary-Dev/viewbuild.php?id=$1&title=$2&page=$3 [L,QSA]
A mabye better variant would be to put the .htaccess into the /BuildSanctuary-Dev/ folder and use a relative path as redirect:
RewriteBase /BuildSanctuary-Dev
RewriteRule ^viewbuild/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ viewbuild.php?id=$1&title=$2&page=$3 [L,QSA]

htaccess rule is not working on localhost

Sorry I changed the previous question. I have problem with .htaccess rewrite rule on localhost, I have .htaccess file in http:// localhost/testing/.htaccess.
I want to change url like below
http://localhost/testing/site.php?site=test
to
http://localhost/testing/test
And I have code in .htaccess as
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ site.php?site=$1 [L]
Which is working correct, but I have also url like
http://localhost/testing/pages.php?site=test&pid=2
Here pages.php with two parameters as site name and page id. I want rewrite this as
http://localhost/testing/test/2
For both conditions I have bellow code which is not working
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ site.php?site=$1 [L]
RewriteRule ^([^/\.]+)/?$ pages.php?site=$1&pid=$2 [L]
Please Help
Thanks :)
What you need to do is turn on the mod_rewrite on your Apache server by performing these steps:
Assuming you have unpacked the xampp folder in the C:\ directory, navigate to the folder containing the apache configuration files.
The full path is C:\xampp\apache\conf\.
In the conf folder, you will find a file named httpd.conf. This file holds all the configuration parameters for apache. Open the file in a text editor.
Search for mod_rewrite.so and you will come across a line as follows : #LoadModule rewrite_module modules/mod_rewrite.so
Uncomment the line by removing the hash (#) mark.
Save the file and restart Apache web server.
and also if the file is already in the testing folder your code should look like this:
RewriteEngine on
RewriteRule ^site\.html$ site.php?site_id=$1
I got solution which worked for me.
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ site.php?site=$1 [L]
RewriteRule ^([^/]+)/([^/\.]+)/?$ pages.php?site=$1&pid=$2 [L,QSA]
Thanks everyone :)

Redirecting all request to the root directory somewhere else?

Alright, so I've downloaded a CMS for a server I'm setting up and I have some difficulty with the path the files are on.
I have put the CMS in a subdirectory on my server as the root directory is already being used by another CMS. However, the CMS repeatedly uses "/" in the code to link to their files. For example, an image is found using this code:
<img src="/images/banner.png" />
As you know this will not work, because the link above redirects the request to the images folder in the root of the server, not the subdirectory. The CMS also came with a ".htaccess" file, so I immediately thought that I could redirect all requests made using the "/" character to the subdirectory on the server. This is the original ".htaccess" file:
RewriteEngine On
RewriteRule ^index.php(|/)$ content/.php
RewriteRule ^error.php(|/)$ content/error.php
RewriteRule ^housekeeping(|/)$ housekeeping/index.php
RewriteRule ^(|/)$ content/$1.php
RewriteRule ^([a-zA-Z0-9_-]+)(|/)$ content/$1.php
RewriteRule ^rd/([^/]+)(|/)$ /rd.php?id=$1
RewriteRule ^quickregister/start(|/)$ /register/start.php
RewriteRule ^quickregister/step2(|/)$ /register/step2.php
RewriteRule ^quickregister/step3(|/)$ /register/complete.php
RewriteRule ^community/online(|/)$ content/online.php
RewriteRule ^community/vip(|/)$ content/vip.php
RewriteRule ^credits/goldvip(|/)$ content/goldvip.php
RewriteRule ^home/(..*)$ content/home.php?u=$1
RewriteRule ^articles/(..*)$ content/articles.php?story=$1
ErrorDocument 403 /content/error.php
ErrorDocument 404 /content/error.php
I thought that by adding
RewriteRule ^/$ /subdirectory/
the requests to "/" would be redirected, but to no avail. I have checked the apache configuration and it seems that overwriting the config using htaccess files is enabled, so if anyone is able to help me out I'd be very happy.
Thanks in advance!
EDIT:
I came real close to a solution. I inserted this code just below "RewriteEngine on":
RewriteRule ^(.*)/(.*)$ /private_servers/strebbohotel/$2
This returned the following error page when visiting the url "http://mysite.com/private_servers/strebbohotel/content/.php":
Not Found
The requested URL /private_servers/strebbohotel/.php was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
As you can see, it skips the "content" subdirectory for some reason. I believe it has something to do with the other lines of the .htaccess file but I can't figure out which ones. It also could be that I need a more complex regex, but I'm not particularly good with it so if someone could help me further I'd be thankful.
You have to change the .htaccess file on yout root directory - and that will mess with the other CMS. The only solution that comes to my mind is to use combination of RewriteCond's:
RewriteCond %{HTTP_REFERER} my_better_cms_url_regex
RewriteCond %{REQUEST_URI} ^/images/.*$
RewriteRule ^.*$ /subdirectory/%{REQUEST_URI}
That should do the trick. If you want to customize it, refer to this cheatsheet (or simmilar). But be aware, that .htaccess in root directory is checked against ALL requests made to your subdir cms - therefore the need for second condition.
The .htaccess file in your CMS subdirectory would have no effect on requests made against the server root directory. You would need to add an .htaccess there.
Even then though, I don't know how you would differentiate requests from the CMS (like the image request in your example) from those intended for the application in the web root.
Does your CMS not have any configuration setting that allow you to specify a path the the files other than the web root?
You want to use:
RewriteBase /another_root_dir/
in the beginning of your .htaccess file

Web site in pure PHP with clean URL

I have enabled mod_rewrite in my Xampp Apache. When I run my phpinfo() page, I saw mod_rewrite under Loaded Modules. So I think it's enabled.
Then I create a folder clean-url under htdocs. Inside clean-url folder I have 3 files
1) index.php here I put
Welcome
2) Test. php
3) .htaccess
Here I put
RewriteEngine On
RewriteRule ^([a-z]+)/([a-z\-]+)$ /$1/$2.php [L]
I want to run the index page, and by clicking on that hyper link I want to display the test.php page with URL
mydomain/clean-url/test
I know I am in a wrong path. What am I doing wrong? Also I don't know any idea about URL rewriting and .htaccess.
There is a dash in clean-url, but your regex does not recognize it.
Change your .htaccess to
RewriteEngine On
RewriteRule ^([a-z-]+)/([a-z-]+)$ /$1/$2.php [L]
and place it in htdocs/ instead of htdocs/clean-url/.
i think you shouldn't use /$1/$2.php, just simply /$2.php
because you ask the rewrite to access the folder clean-url and open test.php in that folder.
try
^(.*)/(.*)$ /$1.php [L]
and see how it works ... then you should format in more complex regex.

Categories