Web site in pure PHP with clean URL - php

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.

Related

How do I get mod-rewrite to access the proper directory?

I'm trying to use mod-rewrite to eliminate urls like {root}/index.php?action=about&page=faq. I want {root}/about/faq to redirect to the php url. My .htaccess file looks like
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^(index|about|calendar|news|contact)$ index.php?action=$1 [L]
RewriteRule ^(index|about)/(.+)$ index.php?action=$1&page=$2 [L]
When redirecting to index.php?action=$1, this works fine. However, it doesn't work completely when the second rule is used. The html content appears fine, but the stylesheet and images are missing. The page seems to think that it's in a subdirectory. So {root}/about/faq looks for images in the folder about/images, instead of in the images folder found at the root. The PHP script says that the working directory is still the root directory, however. How do I make the pages think that they are in the root directory? Thanks!
RewriteRule ^(index|about|calendar|news|contact)/(.*)/$ /index.php?action=$1&page=$2
Would result in /about/faq/ rewriting to index.php?action=about&page=faq
/calendar/month/ would result in index.php?action=calendar&page=month
Also unless you want to rewrite imagesfiles I would add:
RewriteCond %{REQUEST_URI} !(\.gif|\.jpg|\.png)$ [NC]
This will stop image requests being rewritten.

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

PHP Clean URLs not working as intended

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?

How to redirect in url by using .htaccess in php?

How to redirect in url by using htaccess in php?
http://www.example.com/randomstring/2
i would like to go to id page 20 thank
You should use mod_rewrite for example in this way:
RewriteEngine On
RewriteRule ^(randomstring)/([0-9]+)$ /randomstring/$1/ [QSA,R]
RewriteRule ^(randomstring)/([0-9]+)/$ index.php?page=$1&id=$2 [QSA,NC,L]
If you want to simply redirect from a domain to another just write:
RewriteRule ^(randomstring)/([0-9]+)$ http://www.example.com/$1/$2/ [QSA,NC,L,R=301]
use mod_rewrite, it's a module for apache. You can see the documentation here. .htaccess files control the webserver, not php, so you have to look there.
Copy that into your .htaccess file in the directory you need it:
RewriteEngine On
RewriteRule ([A-Za-z0-9]+)/([0-9]+)$ index.php?id=$2
www.domain.tld/asdf/2 --> index.php?id=2
www.domain.tld/asdfa923als/52 --> index.php?id=52
;-)

htaccess mod_rewrite help

i have a domain with a website in a folder.
i.e
http://domain.com.au/website/page.php
i only have access to .htaccess files to make the rules.
i would like my url to look like this..
http://domain.com.au/page
so in essence i want to drop the subfolder its sitting in, and the php extention.
All links in my php are structured like this though
page link
this is the same for js and css. they are all referenced from the 'website' folder.
will the rewrite mean i have to change all my links?
priority is dropping the folder from the url, not so much the php extension.
1) You could move all the files to the root web directory and run the website from there.
2) You would have to manually modify every link to remove the website directory from the beginning and then use the .htaccess file to redirect the pages back to the appropriate page. If you're going to do it this way, you might as well remove the PHP extension...
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^domain.com.au$ [NC]
RewriteRule ^(.*)/ website/$1.php [L]
You may have to modify the website/$1.php part to include your entire root directory in some cases, but that should work as long as the .htaccess file is in the root web directory. That will redirect any page in the form 'http://domain.com.au/page' to 'http://domain.com.au/website/page.php' while keeping the URL the same in the address bar.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^page\_one/(.*)/([0-9]{1,6})/?$ webroot/products/products/view/$2 [L]
</IfModule>
this htaccess code redirects all trafic from www.domain.com/page_one/anything/numeral
to the page www.domain.com/webroot/products/products/view/same numeral
for your example, something like
RewriteRule ^page?$ website/page.php$2 [L]
note, this works for "page" as a string, if you need other functions , change it
for more insight, you might want to take a look at this link :
http://www.javascriptkit.com/howto/htaccess.shtml

Categories