I have a sub directory which has a bunch of .php files in it. I currently have this as my .HTACCESS to rewrite the urls cleanly.
RewriteEngine on
Options +FollowSymLinks
RewriteBase /mailing_list
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
This is fine because it turns http://www.example.com/mailing_list/home to look at http://www.example.com/mailing_list/home.php
What I also need to account for is a page using clean urls for the get variables. That page is called w.php and it passes ?i=(int)
When I try to access http://www.example.com/mailing_list/w/1 I get a 505 server error. What do I need to add to my htaccess to account for this? Is there a full proof way which will account for others I run in to. For example:
What if a page comes up called blogs and it passes the ID of the blog like this: http://www.example.com/mailing_list/blog/1
Will I have to add a new Rule for that page specifically?
Replace
RewriteRule ^(.*)$ $1.php
by
RewriteRule ^([^/]+)$ $1.php [L,QSA]
RewriteRule ^([^/]+)/(.*)$ $1.php?i=$2 [L,QSA]
Related
Does anyone know how i can stop variables from being lost after creating a Rewrite Rule?
Putting in [QSA] gives me a Server 500 error message
I want this rule
RewriteRule ^family-name/([^/.]+)/([^/.]+)$ family-name.php?familyName=$1&token=$2 [L]
and this works in the sense of i get
family-name.php/somthing/token
However, when i go to $_GET['familyName'] or token it returns blank. why?
Note. the ReWrite rule is in my .htaccess page
UPDATE:
I have reduced my code down to this:
RewriteEngine On
RewriteRule ^my-family/family-name/([a-z]+)/([a-z0-9]+)$ my-family/family-
name.php?familyName=$1&token=$2 [QSA,L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)$ $1.php [NC,L]
My parameters are being passed through onto the next page LOCALLY ONLY and not when uploaded to my live site. Any thoughts???
You should open page (with your domain name).
http://example.com/family-name/something/token
And call your parameters with $_GET['token'], not $_SESSION['token'].
If you want RewriteRule for
http://example.com/family-name.php/something/token
You should have:
RewriteRule ^family-name.php/([^/.]+)/([^/.]+)$ family-name.php?familyName=$1&token=$2 [L]
I am trying to remove the .php extension at the same time as creating a "pretty URL" for a certain page.
I am trying to turn domain.com/dox?id=3 into domain.com/3
Current rewrite conditions in my htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]*)\$ /dox?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
This creates an error at domain.com/3 that says:
Not Found
The requested URL /3.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.
However if I add any extension to the code, it works. To explain what I mean, if I change the fourth line to this:
RewriteRule ^([^/]*)\.extension$ /dox?id=$1 [L]
Then the page domain.com/dox?id=3 will be accessible at domain.com/3.extension
Obviously the problem is the two rules are conflicting, but not being a htaccess whizz, I can't figure it out
Try this:
RewriteRule ^([A-Za-z0-9-]+)/?$ /dox?id=$1 [L]
Or if id is just a number then this would be more appropriate:
RewriteRule ^([0-9]+)/?$ /dox?id=$1 [L]
I would like to first apologize for my choice of words. I haven't been in the web dev business to properly word the title, so there's that.
I'm using Apache Server and I have a .htaccess file in the root folder of my project. It specifies a 404 page and also a rewrite condition for removing the .php out of a file in the URL:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1\.php
</IfModule>
I have a users section on my page "social.php" and retrieve them using a GET variable in my code.
Example URL:
www.mysite.com/users/social.php?username=someuser
However, I wanted to know how I could make it look something like:
www.mysite.com/users/someuser
Thanks in advance!
Have your root .htaccess as this:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteRule ^users/(\w+)/?$ /users/social.php?username=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1.php [L]
</IfModule>
This is only partly a mod_rewrite problem at this point.
The first part is to route URLs like /users/[a-z]+ to some script /social.php
RewriteRule ^/users/(.*)$ social.php?username=$1
The second part is inside social.php you need to get the username "someuser" in $_GET['username'].
1) I have a Drupal site located at http://example.com and I have a directory located at http://example.com/foo, but I also have a Drupal page with an alias of http://example.com/foo. How can I get the Drupal page to serve? Currently, I get a 403 forbidden page as a result of the Options -Indexes declaration in Drupal's .htaccess file, but I do not want to remove this as I do not want directories to be browsable.
EDIT: I have solved this with the following rule:
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ http://localhost/s2k/index.php?q=$1 [L,QSA]
2) To make the problem even more difficult, given the same scenario, if I have an index.html file inside the directory http://example.com/foo/index.html I always want this to take priority over Drupal's aliased page (which it does at the moment - currently I have modified the .htaccess file so that any directory with an index.html file displays it - DirectoryIndex index.php index.html).
EDIT:
So now, how can I write a RewriteCond that will look to see whether or not there is an index.html file inside the directory?
I am no RewriteRule Guru and pretty sure there is a nicer way to achieve this, e.g. with conditional RewriteCond.
But simply adding
RewriteCond %{REQUEST_URI} =/foo/
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Above the default rewrite Conds and rules make this rule kick in for /foo/ and not the Drupal-default one. Where a RewriteCond %{REQUEST_FILENAME} !-d will only apply the RewriteRule if something is NOT a dir.
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteRule ^(.*)$ http://localhost/s2k/index.php?q=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME}index.html -f
RewriteRule ^(.*)$ $1index.html [L]
RewriteCond %{REQUEST_FILENAME}/index.html -f
RewriteRule ^(.*)$ $1/index.html [L]
[normal Drupal rewrite rules]
I currently use the follwoing code in my .htaccess file
RewriteEngine On
RewriteBase /
# Redirect languages
RewriteRule ^(en|es|zh\-tw|zh\-cn)/?$ index.php?lang=$1 [L]
With that code, every time I type for instance, /en at the end of the URL, it redirects me to /?lang=en (loads the the English content from a PHP array):
For instance:
example/en redirects me to example/?lang=en while keeping example/en in the URL.
But I also have a thanks.php page and the code above clearly just work for the index.php page.
How can I make the rewrite rule to work for both index.php and thanks.php page?
The most straight-forward way is just to do this:
RewriteRule ^(en|es|zh\-tw|zh\-cn)/?$ index.php?lang=$1 [L]
RewriteRule ^thanks/(en|es|zh\-tw|zh\-cn)/?$ thanks.php?lang=$1 [L]
If you want to make it more general, you have the option of white-listing files, like this:
RewriteCond $1 ^(thanks)/$ [OR]
RewriteCond index ^(index)$
RewriteRule ^(.+/)?(en|es|zh\-tw|zh\-cn)/?$ %1.php?lang=$2 [L]
...where (thanks) would be a pipe-delimited list of the files you wanted to have this functionality, or you can just accept every request as a pass-through to an existing PHP page:
RewriteRule ^(en|es|zh\-tw|zh\-cn)/?$ index.php?lang=$1 [L]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?[^/]?)/(en|es|zh\-tw|zh\-cn)/?$ $1.php?lang=$2 [L]