Rewriting in .htaccess with forward slashes - php

here is what I need. I have the url pearlsquirrel.com/profilecomments.php?u=eggo.
eggo being my username and the part of the dynamic url that changes. I would like to rewrite the url to say pearlsquirrel.com/eggo/comments, using .htaccess.
Here is what I have so far:
RewriteRule ^(.*)$ $1.php
RewriteRule ^([a-zA-Z0-9_-]+)$ profilecomments.php?u=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profilecomments.php?u=$1
RewriteCond %{HTTP_HOST} ^www\.pearlsquirrel\.com$ [NC]
RewriteRule ^(.*)$ http://pearlsquirrel.com/$1/comments [L,R=301]
but I just can not get it to work. Any help would be greatly appreciated. Thanks!

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/comments$ profilecomments.php?u=$1 [L]
NOTE
If you've used relative paths for your images, stylesheets etc. you need to change those into absolute paths or relative paths using the servers root folder as base in order to get your site to display properly.
For example, it will think that images/image.png is /eggo/comments/images/image.png
But, if you instead add a preceding slash /images/image.png your file paths will always start from the servers root folder and your site won't get messed up when you're rewriting your URL's.

Your first rule, overrides all the rest.
What you are describing (if I understood correctly), you need to handle /user/comments by profilecomments.php?u=user
RewriteRule ^([a-zA-Z0-9_-]+)/comments profilecomments.php?u=$1 [L]
this should do it.

Related

.htaccess file - Wildcard Subdomains Redirect

I have got a lot of subdomains configured using wildcard e.g.
subdomain1.domain.com
subdomain2.domain.com
subdomain3.domain.com
(...)
subdomain89.domain.com
etc etc.
They are point to /public_html/. Inside the public_html I have created
/public_html/subdomain1
/public_html/subdomain2
/public_html/subdomain3
(..)
/public_html/subdomain89
sub-folders.
I would like to redirect all request from subdomains (any) to index.php files within the respective sub-folders e.g.:
http://subdomain1.domain.com/
http://subdomain1.domain.com/about_us.php
http://subdomain1.domain.com/contact.php
redirects to /public_html/subdomain1/index.php.
http://subdomain2.domain.com/
http://subdomain2.domain.com/about_us.php
http://subdomain2.domain.com/contact.php
redirects to /public_html/subdomain2/index.php etc etc.
This is my .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.domain\.com$ [NC]
RewriteRule !^([a-z0-9-]+)($|/) /%2%{REQUEST_URI}/index.php [PT,L]
When I access subdomain1.domain.com i see the index.php file from /public_html/subdomain1 but when I access subdomain1.domain.com/about_us.php i got 404. Any ideas?
Thanks
I have figured it out. This is the working code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.domain\.com$ [NC]
RewriteRule !^([a-z0-9-]+)($|/) /%2%{REQUEST_URI}/ [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
:-)
First make sure that your .htaccess file is in your document root (the same place as index.php) or it'll only affect the sub-folder it's in (and any sub-folders within that - recursively).
Next make a slight change to your rule so it looks something like:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?path=$1 [NC,L,QSA]
At the moment you're just matching on . which is one instance of any character, you need at least .* to match any number of instances of any character.
The $_GET['path'] variable will contain the fake directory structure, so /mvc/module/test for instance, which you can then use in index.php to determine the Controller and actions you want to perform.
If you want the whole shebang installed in a sub-directory, such as /mvc/ or /framework/ the least complicated way to do it is to change the rewrite rule slightly to take that into account.
RewriteRule ^(.*)$ /mvc/index.php?path=$1 [NC,L,QSA]
And ensure that your index.php is in that folder whilst the .htaccess file is in the document root.
Alternative to $_GET['path'] (updated Feb '18 and Jan '19)
It's not actually necessary (nor even common now) to set the path as a $_GET variable, many frameworks will rely on $_SERVER['REQUEST_URI'] to retrieve the same information - normally to determine which Controller to use - but the principle is exactly the same.
This does simplify the RewriteRule slightly as you don't need to create the path parameter (which means the OP's original RewriteRule will now work):
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /index.php [L,QSA]
However, the rule about installing in a sub-directory still applies, e.g.
RewriteRule ^.*$ /mvc/index.php [L,QSA]

Remove /page/ in url when opening pages

We have added a paging system inside our layout. When we go to /page/clan, the page about our clan gets displayed. (as its located in pages/clan.php).
To get /page, we used a htaccess script, which rewrites index.php?page=pagename into the /page/pagename I mentioned.
This is our current htaccess code for converting these urls:
RewriteEngine On
RewriteRule ^page/([^/]*)$ index.php?page=$1 [L]
However, We'd like to remove the /page part, so it's possible to just use /clan instead of /page/clan to open the clan page.
How can this be done and with what code?
Thanks!
Try :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/?$ index.php?page=$1 [L,NC]
Rewrite condions make sure you don't rewrite any existing files or directories on the server.
[NC] flag makes the pattern case insensitive, so in you case example.com/fOo would also work.

Remove file extention from urls

I have some urls in my website like this..
http://www.mydomain.com/about.php
http://www.mydomain.com/contact.php
http://www.mydomain.com/signup.php
http://www.mydomain.com/testimonials.php ...... and much more
Now I am trying to convert above urls to user friendly url
http://www.mydomain.com/about
http://www.mydomain.com/contact
http://www.mydomain.com/signup
http://www.mydomain.com/testimonials
This is code in my .htaccess file that I am tried so far. But it doesn't work.
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
Anyone can help me to remove .php etension from my urls??
Thank you.
Get rid of the conditions you are using and instead use the below 2 lines in your htaccess file
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^whatever/?$ whatever.php [NC]
This will work even if user types a forward slash after the name or not
If you want to make them dynamic, here's a good tutorial on that
for a proper implementation so as not cause confusion during deployment if you dont have access to the htaccess file of your server or if it is override. You can consider a library like toro-php http://toroweb.org/ for easy routing.

How to rewrite URL to include variables and remove php while allowing directories

I have the following htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/?user=$1
which works as expected for example
http://example.com/index.php?user=USERNAME -> http://example.com/USERNAME
However I have created a form on the page index.php which posts to /directory/save.php
How do I remove .php while allowing for the directory so that I can post to /directory/save/ instead?
if it is going to one and only such file in /directory then probably hard code it by adding following before the above rules?
RewriteRule ^directory/save$ /directory/save.php [L]
directory/ might have it's own rewrite rules and not have a physical save.php, that's why !-f might not work. Try adding a new rewrite condition to stop rewriting for directory/
RewriteCond %{REQUEST_URI} !^directory
Try to check this one if it's rewriting /directory/save.php file to directory /directory/save/
RewriteCond %{REQUEST_URI} ^/directory/save\.php$
RewriteRule ^(.+) /directory/save/ [NC]

Rewriting example.com/section.php?username=xyz to example.com/xyz/section

Using the following htaccess, I have been able to rewrite example.com/profile.php?username=xyz to example.com/xyz,
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?user=$1
Adding the following to the above,
RewriteRule ^([a-zA-Z0-9_-]+)/section$ section.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/section/$ section.php?user=$1
did not resolve example.com/section.php?username=xyz to example.com/xyz/section.
Am I doing something wrong here?
First of all: The manner of speaking would rather be the opposite: The rules you showed are to rewrite requests like /xyz internally to /profile.php?username=xyz and not vice versa.
Now if you want to rewrite requests like /xyz/section internally to /section.php?username=xyz where section and xyz are variable, try these rules:
RewriteRule ^([a-zA-Z0-9_-]+)/?$ profile.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/([^/]+)/?$ $2.php?user=$1
To look for static files (images, css) in the right directory without having to write the file address, do:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Before writing the code suggested by RageD
(Sorry, should have posted it as a comment but I needed newlines)
I tested it and it works. Try this:
RewriteRule ^([a-zA-Z0-9_-]+)\/section$ section.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)\/section\/$ section.php?user=$1
All I did was escaping the / in the URL since / is a regex delimiter.

Categories