Url Rewriting with hidden rules - php

I'm currently facing a problem with one url rewriting rule.
[I'm running on Apache 2.4.23]
I'm trying to make a redirection for certain pages. Here are my rules :
RewriteRule ^clubs[^/]?$ club.php?job=display [L,NC]<br />
RewriteRule ^club/([^/]*)$ club.php?job=detail&name=$1 [L]<br />
RewriteCond %{REQUEST_URI} !club\.php [NC]<br />
RewriteRule ^club/([^/]+)/?(.+)?/? club.php?job=detail&name=$1&action=$2 [NC,L]
The problem is that rule for "clubs" is working well. But those for club aren't working.
If I go to the url http://mysite/club , I can see that it has already been rewrited to http://mysite/club.php?
And if I go to the url http://mysite/club/clubName , I can see that it has already been rewrited to http://mysite/club.php/clubName?
And all of this because the file club.php exists. If I had redirected 'club' to 'anythingElseButNonExistingFile' , it would have worked.
Even if I remove all the lines in my .htaccess file, this happens. So I suppose there's another .htaccess file with other rules somewhere I do not have access to.
So I changed my rules for 'club' to :
RewriteRule ^club.php/(.+) club.php?job=detail&name=$1 [L]<br />
RewriteCond %{REQUEST_URI} !club\.php [NC]<br />
RewriteRule ^club/([^/]+)/?(.+)?/? club.php?job=detail&name=$1&action=$2 [NC,L]<br />
So now, if I try to go to :
http://mysite/club/clubName, it works, it redirects to http://mysite/club.php/clubName?job=detail&name=clubName
Not nice but working...
But if I try to go to http://mySite/club/clubName/addPhoto, url is not properly rewritten. I get :
http://club.php/clubName/addPhoto?job=detail&name=clubName%2FaddPhoto
I don't know how to get rid of this...
I hope this is not too much confused and that someone will have a solution... Thanks in advance !

But those for club aren't working. If I go to the url http://mysite/club , I can see that it has already been rewrited to http://mysite/club.php?
You need to disable content negociation by Options -MultiViews in your .htaccess.
And if I go to the url http://mysite/club/clubName , I can see that it has already been rewrited to http://mysite/club.php/clubName?
This is the fault of the combination of content negociation and PathInfo feature (see AcceptPathInfo directive). The PathInfo feature allows an extra (and unexistant) path behind the file (/clubName in your example) while the content negociation internally rewrite club into club.php.
So to not be bothered when doing rewriting, it might be a good idea to eliminate both with these statements (in your .htaccess file):
Options -MultiViews
AcceptPathInfo off

Related

Having some issues with htaccess mod rewrite

Hello everyone wondering if i can pick your brains and il be as descriptive as possible.
So i am trying to create a page that will load a title and category (dynamic from database) based upon the selection that is picked by the user
sitename used as my domain name
So to be a little clearer i have a file and variables named sitename/2017/slot.php?gp=category&sn=title
the category and title are stored in the database for each row what i want to do is make a clean URL to look like this sitename/2017/slot/category/title/
So when viewing the page like this sitename/2017/slot.php?gp=category&sn=title the page loads everything in and displays correctly and the variables are being set as i can see them in my title in the browser tab
my error comes when i use the mod rewrite to make it clean, once the .htaccess has been changed and uploaded and i navigate to the new clean URL sitename/2017/slot/category/title/ it shows me the variables havent been set as they no longer appear in the title tab of the browser and also the page doesnt use my css. Below i have added my htaccess rule, any help and feedback is greatfully appreciated
#change this sitename/2017/slot.php?gp=category&sn=title
#to this sitename/2017/slot/category/title/
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^slot/([a-z-]+)/([a-z-]+)/ slot.php?gp=$1&sn=$2 [NC,L]
just incase here is the php code im using in slot.php i am connecting to my database fine no issues there and after the code below im running a while loop to get out the data for the row the user clicked on
if($_GET['gp'] && $_GET['sn']){
$gp = preg_replace('#[^a-zA-Z-]#i', '', $_GET['gp']);
$sn = preg_replace('#[^a-zA-Z-]#i', '', $_GET['sn']);
}
here is the full code i have in my htaccess
#make custom page not found
#ErrorDocument 404 /filenotfound.php
#stop directory file listing in all folders
IndexIgnore *
#make pages have www. in them
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.org
RewriteRule ^(.*)$ http://www.example.org/$1 [R=permanent,L]
#change this slot.php?gp=category&sn=title
#to this sitename/2017/slot/category/title/
Options -MultiViews +FollowSymLinks
RewriteRule ^slot/([a-z-]+)/([a-z-]+)/ /2017/slot.php?gp=$1&sn=$2 [R=302]
If your URL starts with 2017 you must include it in your RewriteRule.
RewriteRule ^2017/slot/([a-z-]+)/([a-z-]+)/ slot.php?gp=$1&sn=$2 [NC,L]
You're misusing the ^, which matches the very start of the path (the part after sitename/. That is, a rule starting with ^slot/ will match only sitename/slot/, not sitename/2017/slot/. Your rule should look like this:
#change this sitename/2017/slot.php?gp=category&sn=title
#to this sitename/2017/slot/category/title/
RewriteRule ^2017/slot/([a-z-]+)/([a-z-]+)/ slot.php?gp=$1&sn=$2 [NC,L]
May I join the party?
RewriteRule ^sitename/2017/slot/([a-z-]+)/([a-z-]+)/ /slot.php?gp=$1&sn=$2 [NC,L]
If the sitename the domain name, I will change my answer to :
RewriteRule ^2017/slot/([a-z-]+)/([a-z-]+)/ /slot.php?gp=$1&sn=$2 [NC,L]
Which is almost the same as Dekel's, but not exactly :)
#example.com/2017/slot.php?gp=category&sn=title
#example.com/2017/slot/category/title/
You'll need to disable MutliViews. Near the top of your .htaccess file:
Options -MultiViews
With MultiViews enabled (part of mod_negotiation) /2017/slot is being implicitly rewritten to /2017/slot.php (without any query string paramaters) before your mod_rewrite directives are being processed.
Although it's still not clear where your .htaccess file is located. So, there may still be some work to do... If your .htaccess file is located at example.com/2017/.htaccess then your existing mod_rewrite directives should be sufficient.
However, if your .htaccess file is in the document root (ie. example.com/.htaccess) then you will need to modify your RewriteRule:
RewriteRule ^2017/slot/([a-z-]+)/([a-z-]+)/ 2017/slot.php?gp=$1&sn=$2 [NC,L]

Using htaccess to remove trailing slash

I have a url "www.example.com/abc/abc.php"
If I change it to "www.example.com/abc/abc.php/asdasd", some kind of infinite loop starts and the server's memory peaks to 100%.
I have heard that there's some way by .htaccess by which I can redirect any "abc.php/asdasd" to "abc.php" only. Please help how, as I am not able to understand it from other examples mentioned on net.
NOTE : I dont want this "/" to be removed if it is put at the end of directories though.
This would redirect one URL to another:
Redirect 301 /abc.php/adssd http://www.example.com/abc.php
However, this will only handle this one example. You should post your full .htaccess file so we can see what's really going on
I was able to achieve what I wanted by the following htaccess code :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)\.php/(.*) $1.php [F]
</IfModule>
This may not be perfect, but gets the job done. Now anything after the text 'abc.php' results in a page-not-found.
Works for cases like :
www.website.com/abc.php/
www.website.com/abc.php/asd
www.website.com/abc.phpasd
www.website.com/abc.php?
etc.

Infinite redirect loop issue htaccess

I am trying to redirect from one url to another using a htaccess file. I have got them all working except from one which causes an infinite redirect loop. The url I wish to redirect from is:
http://website.co.uk/author/ and i want to redirect to http://website.co.uk/author/authorname
Any ideas would be helpful
Sounds a lot like your .htaccess redirect rules are doing pattern matching on your domain name, so that when you redirect to /jamescrawford it matches against www.pragencyone.co.uk/author/ and tries to redirect again.
If you're trying to catch everything that matches http://www.pragencyone.co.uk/author/.* then you'll need to exclude http://www.pragencyone.co.uk/author/jamescrawford (and potentially any assets it uses like images, if they're in the same directory) from being matched by the pattern you're using.
You might try posting the actual rules that you're using to do the redirect, though obviously be sure not to post anything any info from your .htaccess that would compromise your server's security.
try this in your htaccess file
RewriteEngine On
DirectoryIndex index.php
AddDefaultCharset On
Options +FollowSymLinks -Indexes
RewriteRule ^author /author/jamescrawford [L]
RewriteRule ^author/ /author/jamescrawford [L]

Can't access post or get from a php page after redirecting

I'am redirecting about 100 hmtl pages to a single PHP page (example.php) using .htaccess. It is working perfectly.
I've pagination on that page (example.php) but I am using the original HTML page URL (example.html?page=2&limit=20)
so example.html, example1.html, example2.html, example3.html are all redirecting to example.php.
The address bar is still showing ".html" URL but due to .htaccess redirection the example.php is rendering.
when is click on a pagination link (example.html?page=2&limit=20) the browser address bar shows correct .html URL and query string.
I've tried to get the values of page, and limit using $_GET and $_REQUEST in (example.php) but i am not successful.
Please help me in reading the (example.html?page=2&limit=20) query string parameeters .
Edit Code ported from comments:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !^page-(.*)$
RewriteRule ^page-(.*)$ size-content.php?sef=$1 [L]
Add the QSA flag, which means "query-string append" to be sure the existing query string is ported into the rewritten URL.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !^page-(.*)$
RewriteRule ^page-(.*)$ size-content.php?sef=$1 [L,QSA]
.htaccess modify your server configuration.
if you are making redirection then you change your request.
Try mod_rewrite if you are using Apache of course.
RewriteEngine On
RewriteRule ^example\.html\?(.*) example.php?$1
Mod rewrite is module to Apache. It is not allowed on most free hostings.
Yasir - you can resolve this problem by two ways:
1) Re-write rules for .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !^page-(.).html(.)/(.)$
RewriteRule ^page-(.).html(.)/(.)$ size-content.php?sef=$1&page=$2&limit=$3 [L]
This rule will handle: page-example.html?page=2&limit=20
I hope - you will easily understand the above rule.
Note: Keep one thing in your mind that every link should be in same pattern if you change rule in htaccess.
2) You can resolve this problem on your "size-content.php"
Suppose page-example.html?page=2&limit=20
$_GET['sef'] = example.html?page=2&limit=20 [according to you .htaccess]
Now you can parse this string via explode function
Thanks

remove http from URL in .htaccess (mod_rewrite)

Been stuck on this for ages and tried loads of fixes but just can't get my head around it!
I run a site where the content of the pages are generated based upon a URL. For example:
http://www.mysite.com/http://www.supercheese.com
Would generate a mashup of content from mysite.com and supercheese.com
The .htaccess i use for this (at mysite.com) is:
<ifModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteRule assets/.* - [L]
RewriteRule ^(.*)$ test.php?url=$1 [NC,L]
</ifModule>
So basically the second URL is passed in a php string.
My question is this, I need to remove the http:// from the address using .htaccess
E.G. If someone types:
http://www.mysite.com/http://www.supercheese.com
I need it to become:
http://www.mysite.com/www.supercheese.com
Many thanks in advance for taking a look at this. You guys rock.
You can simply use a RedirectMatch
http://httpd.apache.org/docs/2.0/mod/mod_alias.html#redirectmatch
Try with :
RedirectMatch ^/https?://(.*)$ http://www.mysite.com/$1
Edit : you have to put this before rewrite rules
Edit : add / before http
Edit : David is right, take a look a his answer and change your way of writing these urls
It looks to me like the url scheme is inherently problematic.
Using an unencoded colon : in the url - other than following the http to specify the access protocol - seems to make the server think that it is doing authentication of the form http://username:password#hostname/.
I know it doesn't directly answer the question, the solution is to change the url-scheme. ;-(

Categories