I got this problem, where I try to make an Image Archive. Most of it works, but I have now come to prettifying URLs. I have worked with this before, but somehow, it don't work now. If I add more then one Rewrite URL to the same file (index), it ignores the values added with it.
What I got:
Header add "disablevcache" "true"
RewriteEngine On
RewriteBase /folder/
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteRule ^archive/([^/]+)?$ archive/index.php?cat=$1 [E=ORIG_URI:/$1]
RewriteRule ^archive/([^/]+)/([^/]+)?$ archive/index.php?cat=$1&sub=$2 [E=ORIG_URI:/$1]
RewriteRule ^archive/([^/]+)/([^/]+)/([^/]+)?$ archive/index.php?cat=$1&sub=$2&id=$3 [E=ORIG_URI:/$1]
RewriteRule ^archive/([^/]+)/([^/]+)/([^/]+)?/([^/]+)$ archive/index.php?cat=$1&sub=$2&id=$3 [E=ORIG_URI:/$1]
The following should allow me to access these URLs:
domain.com/archive/category
domain.com/archive/category/subcategory/
domain.com/archive/category/subcategory/id
domain.com/archive/category/subcategory/id/item
domain.com/archive will be made default, since I use an index.php file, in a folder. And item will just be for SEO and has nothing to do with PHP. (The last rewriteRule)
This works. I can access the same page, from all the different URLs. (archive/index.php). But when I try to use PHP to get the value from the provided URLs, it won't work. I can't even check and see if there are values.
No matter what I do, it just echo out Category & Sub Category on every URL. Also if I go to the default URL, domain.com/archive.
if($_GET['cat']){
echo 'Category';
if($_GET['sub']){
echo 'Sub Category';
}
}
Anyone who can see what I am doing wrong or got a suggestion to why it ignore my values? Or maybe know another way to do this, so I end up with the same result?
Apache rewrites in a loop. You need to stop it for archive/index.php since that always matches the first RewriteRule:
RewriteRule ^archive/index\.php$ - [L]
RewriteRule ^archive/([^/]+)?$ archive/index.php?cat=$1 [E=ORIG_URI:/$1]
RewriteRule ^archive/([^/]+)/([^/]+)?$ archive/index.php?cat=$1&sub=$2 [E=ORIG_URI:/$1]
RewriteRule ^archive/([^/]+)/([^/]+)/([^/]+)?$ archive/index.php?cat=$1&sub=$2&id=$3 [E=ORIG_URI:/$1]
RewriteRule ^archive/([^/]+)/([^/]+)/([^/]+)?/([^/]+)$ archive/index.php?cat=$1&sub=$2&id=$3 [E=ORIG_URI:/$1]
You can also remove [E=ORIG_URI:/$1] if you don't use it.
Related
This is the full set of rewrite code in my htaccess file, but I'm only having problems with a conflict in the last 3 rules. The final rule writes the URL I want, but returns travel.php instead of travel2.php
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{http_host} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
RewriteRule ^(.+[^/])/$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^journal/([^/]*)$ /journal2.php?url=$1 [L]
RewriteRule ^travel/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)$ travel.php?country=$1&url_string=$2
RewriteRule ^travel/([a-zA-Z0-9-]*)$ /travel2.php?cat=$1
RewriteRule ^travel/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)$ travel2.php?cat=$1&subcat=$2
This is what I'm after from those last 3:
/travel/country/title-url-string (currently works & displays correct content)
/travel/category (currently works & displays correct content)
/travel/category/subcat (currently displays URL as I want it, but returns travel.php content instead of travel2.php)
I originally had an ID number in the travel.php rule, and removing that has resulted in the conflict. I'm aware two of the rules have the same pattern, so how can I best go about getting the results I want? Thanks for any help. :)
From last 3 rewrite rules:
1 & 3 check the same pattern where first one always take effect.
Further, as your category and country values cannot be distinguished each other here, I would suggest to append a unique identifier to the url as below:
RewriteRule ^travel/country-([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)$ travel.php?country=$1&url_string=$2
RewriteRule ^travel/category-([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)$ travel2.php?cat=$1&subcat=$2
So your matching urls would be:
/travel/country-countryname/title-url-string
/travel/category-categoryname/subcat
My URLs look like http://example.com/?n=x. I want the URL to show as http://example.com/. I have used to approaches so far and none of them works.
First one is based on this question:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^n=1$
RewriteRule (.*) $1? [R=permanent]
After the answer below I modified the .htaccess file:
```RewriteEngine On
RewriteCond %{QUERY_STRING} ^n=(.*)$
RewriteRule (.*) $1? [R=permanent]```
but it still did not work. Here is the debugging info:
RewriteCond %{QUERY_STRING} ^n=(.*)$
This condition was met.
RewriteRule (.*) $1? [R=permanent]
The new url is http://example.com/blog/cat/??
Test are stopped, a redirect will be made with status code permanent
Second approach is:
RewriteEngine On
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(.*)$ $1 [QSD]
None of them seem to work.
I have a few more questions:
Does rewriting only work if the URL is not typed manually?
Can I still access the values of query strings in PHP?
What happens when a user copies the rewritten URL?
The first approach is correct if you go to http://example.com/?n=1, if I am correct you should change ^n=1$ to ^n=(.*)$.
And the other questions:
It works for all kind. It doesn't matter if it was a robot or a human writing it, when you access a page, .htaccess will be read.
Yes you can. Use $_SERVER["QUERY_STRING"]. If you use the htaccess redirection before explained, you will lose them because you are redirecting.
What do you mean ?
Before anyone comments, I know there are a lot of posts created on this topic, but none of them seem to solve my problem, that is why I have started this thread.
So, I have a page in my website called project.php which is used in GET query like so: project.php?id=12 I want to have a .htaccess file that converts the given URL into localhost/MyWeb/project/id/12/. I've literally followed every single post regarding that topic but none of them seem to work.
Also, along with that, I want all my .php and .html files to be shown just with their names, i.e localhost/MyWeb/index.php/ becomes localhost/MyWeb/index/ and localhost/MyWeb/sub1/sub2.php becomes localhost/MyWeb/sub1/sub2/.
EDIT:
The reason why I did not add my work in first place was because I didn't think it would be any helpful. But here it is:
RewriteEngine On
RewriteRule ^([0-9]+)$ project.php?id=$1
RewriteRule ^([0-9]+)/$ project.php?page=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Firstly, you are operating out of a sub-directory (MyWeb), which means you need to set a RewriteBase. Also, you need to ensure that your .htaccess file is placed inside that sub-directory, and not in the localhost document root.
So, below RewriteEngine on, insert the folloeing line:
RewriteBase /MyWeb/
Next, you stated that you want to convert project.php?id={id} to project/id/{id}, but your code omits the /id/ segment. I also noticed that you have two rules, and that the second one contradicts your question, so I am only going to show you the change you need to make for the first rule, until such time as you clarify what the second rule is for.
To make the project URI work, change the very first rule to:
RewriteRule ^project/id/([0-9]+)/?$ project.php?id=$1 [QSA,L]
This will match the URI you want, with an optional trailing slash. I've also added the QSA flag which appends any extra query string parameters to the rewitten URI, as well as the L flag which stops processing if the rule is matched.
Next, to omit the .php or .html from your URIs, change the last three lines to the following:
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^([^\.]+)$ $1.html [L]
When you make a request to localhost/MyWeb/index, Apache will check to see if localhost/MyWeb/index.php or localhost/MyWeb/index.html exist, and will then serve whichever one it finds first.
If you have both the PHP and HTML files, then the PHP one will be served, and not the HTML one. If you prefer to serve HTML files, then swap the two blocks around.
Unfortunately, I don't know of a good way to force a trailing slash for these, specifically because of the condition that checks for their existence. In other words, it won't work if you request sub2/, with the trailins slash because it would need to check if sub2/.php exists, which it does not.
Update: For added benefit, place these two blocks just below the new RewriteBase you set earlier to redirect the old URIs to the new ones whilst allowing the rewrites to the new URIs to still work:
RewriteCond %{THE_REQUEST} \/project\.php\?id=([0-9]+) [NC]
RewriteRule ^ project/id/%1/ [R=302,L,QSD]
RewriteCond %{THE_REQUEST} \/MyWeb/(.+)\.(php|html)
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ %1 [R=302,L]
For reference, here's the complete file: http://hastebin.com/gacapesoqe.rb
Ok, So I've looked at this topic for quite a while now and can't get anything to work, probably because I'm still having difficulty understanding it - So I'm going back to basics and asking this in the simplest of terms.
I have an empty .htaccess file
I have a current URL of http://www.website.co.uk/news.php?id=111111
I want this to become http://www.website.co.uk/news/111111
How Do I Do This?
Also please not that although this is the URL now, I'm planning on making some changes to the site so the URL's in the future may be:
http://www.website.co.uk/news.php?city=city&issue=1&title=the-title&id=111111
http://www.website.co.uk/news/city/issue/the-title/111111
How can I make it so that the future changes will work too? So far I have:
RewriteEngine On
RewriteRule ^news/(.+)$ news.php?id=$1 [L]
This still displays the full url and typing in news/111111 redirects to an error page. Please help!
Adding the following to your htaccess should work:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^news.php$ /news/%1? [L,R]
RewriteRule ^news/(.*) news.php?id=$1 [QSA]
The above will change http://www.website.co.uk/news.php?id=111111 to http://www.website.co.uk/news/111111
and below will change
RewriteCond %{QUERY_STRING} ^city=(.*)&issue=(.*)&title=(.*)&id=(.*)$
RewriteRule ^news.php$ /news/%1/%2/%3/%4 [L,R]
RewriteRule ^news/(.*)/(.*)/(.*)/(.*) news.php?city=$1&issue=$2&title=$3&id=$4 [QSA]
http://www.website.co.uk/news.php?city=city&issue=1&title=the-title&id=111111 into http://www.website.co.uk/news/city/issue/the-title/111111
The values in %1, %2, $3, %4 gotten from the parameters after city=, issue=. title=. id=.
In city=London, London will be contained in %1 etc
The second RewriteRule will allow you to find the id used.
I have a rewirte rule that rewrites my portfolio urls from
portfolio.php?cat=cat-name&project=project-slug
to
/portfolio/cat/slug/
That all works fine and dandy, I'm wondering if It's possible to add a parameter to the end of the rewritten slug. For example
/portfolio/cat/slug&preview=true
I'm trying to make it so I can preview hidden projects by appending the preview=true to the end. It works if I go to
portfolio.php?cat=cat-name&project=project-name&preview=true
I'm just hoping I don't have to create a rule that has /true I've tied various ways to get the value or preview but thought someone here may have a simple solution
Edit Adding .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^.*$
#portfolio
RewriteRule ^portfolio/?$ portfolio.php [L,NC]
RewriteRule ^portfolio/([a-z0-9_-]+)/([a-z0-9_-]+)/?$ portfolio.php?cat=$1&project=$2 [L,NC]
RewriteRule ^portfolio/([a-z0-9_-]+)/?$ portfolio.php?cat=$1 [L,NC]
RewriteRule ^portfolio/([a-z0-9_-]+)/page=([0-9]+)/? portfolio.php?cat=$1&page=$2&
Add QSA as an option to the back of the RewriteRule (in the [] brackets). E.g.
RewriteRule ^portfolio/([a-z0-9_-]+)/([a-z0-9_-]+)/?$ portfolio.php?cat=$1&project=$2 [L,NC,QSA]
Please look at http://www.workingwith.me.uk/articles/scripting/mod_rewrite for such an example.
You may also include a hidden field with that appropriate value.