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]
Related
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
Im struggling with a .htaccess settings to get following:
To create Multi-language functionality in my CMS without duplicating the productdatabase I would like to get the content of the default language, but keeping the right url in the addressbar.
For example:
http://www.myshop.com/en/webshop get the content of http://www.myshop.com/webshop
http://www.myshop.com/en/collection get the content of http://www.myshop.com/collectie
And also for the products:
http://www.myshop.com/en/webshop/item1 get content of http://www.myshop.com/webshop/item1
http://www.myshop.com/en/collection/product1 get the content of http://www.myshop.com/collectie/product1
In short:
In case of calling the folders '/en/webshop/' and '/en/collection/' there must be a rewrite to '/webshop' and '/collectie'.
Anyone got a clue?
Thanks in advance...
What about:
RewriteEngine on
RewriteRule ^/?en/webshop/(.*)$ /webshop/$1
RewriteRule ^/?en/collection/(.*)$ /collectie/$1
The first line just enables mod_rewrite
The second line (1st rule) matches anything under /en/webshop and discards the /en
The third line is similar to the second but it also "renames" collection to collectie
If you have more folders/paths you want to redirect I 'd suggest adding the rule:
RewriteRule ^/?en/(.*)$ /$1
which is rewriting anything that starts with /en.
Hope it helps, note it is not tested
Use below rule:-
RewriteEngine on
RewriteRule ^en/(.*)$ /$1 [L,R=301,QSA]
OR
RewriteEngine On
RewriteRule ^(.*)/en/(.*)$ $1/$2 [R=301,L]
Hope it will help you :)
In my domain example.com , I want to put all page files in a directory at root for example named file. So for example I put about.php the file directory and want to let users to access this page with this URL : example.com/about
or for example put user directory in the file directory and put login.php in it , and want to let users to access it with : example.com/user/login
In fact I want to remove .php and the file from the URLs.
AND
If those files don't exist , it should load a default file like index.php at root. for example the URL example.com/blabla should be mapped to the index.php
In fact , I want to make two conditions in mod rewrite with the mentioned priority.
notice : of course I should be able to use variables like $_GET at files like about.php
UPDATE : in summary , it should work with this logic :
if the URI isn't a file or directory{
if file/URI.php is a file
load file/URI.php
else
load index.php
}
some body gave an answer to use ErrorDocument 404 index.php , but it's a really bad idea (and now has deleted his answer !)
Thanks for your help...
A conditional rewrite (if the pages exist) sounds like a bad and complicated idea, if it even is possible.
I would rewrite everything to /index.php and control everything from there:
If the components of the url lead to an existing page, use / include that content;
If no valid page is found, present your original index content.
Example redirect rules for your situation:
RewriteEngine on
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^.*$ /index.php?id=$1 [QSA,L]
Will include the requested path in id and append the variables from the original query string (the QSA flag).
More specific for your /user/login example would be:
RewriteRule ^([\w-]+)/([\w-]+)$ /index.php?path=$1&file=$2 [QSA,L]
[\w-+] : any word character (including _) and the -
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]
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