I'm attempting to make a file download site at the moment.
Currently, if you go to localhost/site/file/download/download.php?=CODE it will take
you the designated page with the download link etc.
I'm trying to remove the file/download/download.php part to have
localhost/download/CODE but i can't seem to get it working (so i can use it for my website).
Here is my htaccess file:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /upload_main/file/download/
RewriteRule ^(download)/(\w+)/?$ $1.php?download=$2 [L,QSA]
# to take care of css, js, images
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} \.php$
RewriteRule ^download/(.+)$ /download/$1 [L,R=301]
What am I doing wrong?
Given the changed desired final URL you settled on based on the comments to the question this probably is what you are looking for:
RewriteEngine on
RewriteRule ^/?download/([a-zA-Z0-9_-]+)$ /site/file/download/download.php?=$1 [QSD,END]
That should work in the actual http server's host configuration and likewise in a top level distributed configuration file (".htaccess"). You should definitely prefer the first placement, but there are exceptions where you have to use a distributed configuration file, although that has clear disadvantages.
Related
Evening guys,
I have been attempting to get mod rewrite working for product id's and name's but I am having no luck at all. I have tried searching here on SO but there are no clear explanations.
This is for my ecommerce demo located http://www.peakwebdesigns.co.uk/ecommerce/store
Example URL: http://www.peakwebdesigns.co.uk/ecommerce/product_page?id=3name=Bed
I want: http://www.peakwebdesigns.co.uk/ecommerce/product_page?id=3name=Bed
to be http://www.peakwebdesigns.co.uk/ecommerce/3-Bed
My URL is structured as follows:
<img src="images/'. $product_array[$key]["image"] . '"></div>' ?>
This is what I currently have in my .htaccess (I know the first few code entries work because I have removed .php and .html from files):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^([0-9])-([A-Z]+)$ product_page?id=$1name=$2 [NC,L]
Question: Due to me having /ecommerce/ included in my URL is the above setup correctly?
If not, can anyone help assist me in writing this correctly?
Thanks.
Stan.
There are many possible setups, I expect this to be the one most likely making sense for you (you offered very little information about your situation):
Best is to place such rewriting rules inside the host configuration of your http server:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/ecommerce/(\d+)-(\w+)/?$ /ecommerce/product_page?id=$1name=$2 [END]
Alternatively you can also use a dynamic configuration file. I then would recommend to place that file inside the ecommerce folder and start with something like that:
RewriteEngine on
RewriteBase /ecommerce
RewriteRule ^/?(\d+)-(\w+)/?$ product_page?id=$1name=$2 [END]
For this to work you need to enable the interpretation of dynamic configuration files fist, take a look the the AllowOverride directive in the documentation for that.
Note: in case you receive back an http status 500 (internal server error) with above rules, then chances are that you operate a very old version of the apache http server. In that case try to replace the [END] flag with the [L] flag.
Also, for the sake of precision I want to point out that you need to format the links you send out correctly. You cannot keep the code creating the links unchanged and expect the rewriting module to magically alter the literal links you send out.
And a general hint: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files (.htaccess style files). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only supported as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).
I used the following rules to adjust URLs according to the parameters:
RewriteRule ^ecommerce/product/(.*)$ ./ecommerce/product?id=$1 [L,NC]
RewriteRule ^ecommerce/category/(.*)$ ./ecommerce/category_page?category_name=$1 [L,NC]
I've been trying fix my error, first here is my .htaccess code:
# Clean Url for User Profiles
AddDefaultCharset UTF-8
Header unset ETag
FileETag None
Options +FollowSymLinks -MultiViews
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+friend\.php\?user_name=([^\s&]+) [NC]
RewriteRule ^ friend/%1? [R=301,L]
RewriteRule ^friend/([^/]+)/?$ friend.php?user_name=$1 [L,QSA]
# End Clean Url Htaccess
My intentions with this code is to make this URL http://localhost:8888/circlepanda/friend?user_name=kendrick
Display like this http://localhost:8888/circlepanda/friend/kendrick and work.
The page opens good, burr the parameter isn't parsed. How do I fix this?
There are a number of issues with your code. I tried to fix what I see, here is my version of what you probably are trying to implement:
Options -MultiViews
RewriteEngine on
# external: /..../friend?username=joe >> /..../friend/joe
RewriteCond %{QUERY_STRING} user_name=([^&]+)
RewriteRule ^friend/?$ friend/%1 [R=301,QSA]
# internal: /friend/joe >> friend.php?user_name=joe
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^friend/([^/]+)/?$ friend.php?user_name=$1 [END,QSA]
Depending on your specific situation it might be that you need to add a RewriteBase. See the official documentation for that.
If you experience an internal server error using above lines chances are that you operate a very old version of the apache http server. In that case you need to replace the [END] flag with the [L] flag.
And a general hint: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files (.htaccess style files). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).
I have a serious problem since two days trying to rewrite the urls of my php website with htaccess.
Options +FollowSymlinks
RewriteEngine On
RewriteRule devis demande-devis.php
RewriteRule mentions-legales mentions-legales.php
RewriteRule condition-utilisation condition-utilisation.php
RewriteRule condition-generales-ventes condition-generales-ventes.php
RewriteRule fournisseur fournisseur.php
RewriteRule qui-sommes-nous qui-sommes-nous.php
RewriteRule faq faq.php
RewriteRule services services.php
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !example.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [L,R=301]
But what I expect for example, is to have http://www.example.com/services/ .
With the code, when I type that URL into the browsers address bar, I have a web page without CSS.
Most likely the problem is relative versus absolute notation of the css file urls.
Consult the http servers error log to see what is actually referenced or check the delivered html source of the page that lacks the style definitions.
The page you specify in your comment to the question shows both, relative and absolute references of css files. However you have no rewriting rules for those...
That works as usual. Just ensure you set the right link to your CSS files so that the browser is able to request them without 404's from your server.
As you have probably edited the URLs within the .htaccess files after you've written the php script, you need to reflect these changes in your PHP script, too, by updting the links in there, too.
Monitor your server error log to find out which URIs are wrongly send.
Review those.
Then improve the output of your application by fixing that erroneous output.
Please see as well:
do-it-yourself universal header/footer.php
and similar questions that are about the root of your problem.
I've searched and found a lot of questions on this site and elsewhere that are very similar, but I've tried implementing and modifying all the suggestions I've found and none of it works. I realize this is a very basic question an I am extremely frustrated because nothing I'm trying is working.
With that having been said... I am trying to organize my content pages within kurtiskronk.com/pages/... (e.g. kurtiskronk.com/pages/about.php)
What I want to do is make it so that I can simply link to kurtiskronk.com/about ... So how do I go about stripping "pages/" and ".php"? I don't have a ton of content pages, so it's not a big deal if I have to specify for each page, though something dynamic would be handy.
NOTES: I am using Rackspace Cloud hosting, and WordPress is installed in /blog. My phpinfo() can be seen at http://kurtiskronk.com/pages/phpinfo.php
This is my existing .htaccess file (in the root)
php_value register_globals "on"
Options +FollowSymLinks
RewriteEngine On
#301 redirect to domain without 'www.'
RewriteCond %{HTTP_HOST} ^www\.kurtiskronk\.com$ [NC]
RewriteRule ^(.*)$ http://kurtiskronk.com/$1 [R=301,NC]
RewriteBase /
RewriteCond %{ENV:PHP_DOCUMENT_ROOT}/pages/$1 -f
RewriteRule ^(.*)$ pages/$1 [L]
RewriteCond %{ENV:PHP_DOCUMENT_ROOT}/pages/$1.php -f
RewriteRule ^(.*)$ pages/$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/ blog/index.php [L]
# PHP - MAIL
php_value mail.force_extra_parameters -kurtis#kurtiskronk.com
I tested and the rewrite works with the line below (/about as URL brings up file /pages/about.php), but then the homepage gives a 500 Internal Server Error:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /pages/$1.php [L]
So I'm still sort of in the same boat as before, and as a follow-up, possibly more difficult question, if you go to http://kurtiskronk.com/weddings I am using SlideShowPro (flash) w/ SSP Director (self-hosted) as the back-end for it. When it pulls up a new image, it adds the following after /weddings ... "#id=album-152&num=content-9698"
There are four sections of the portfolio
# Homepage (kurtiskronk.com) id=album-148 ($id is constant for this section)
# Weddings (/weddings) id=album-152 ($id is constant for this section)
# Portraits (/portraits) id=album-151 ($id is constant for this section)
# Commercial (/commercial) id=album-150 ($id is constant for this section)
Assuming we get kurtiskronk.com/weddings to rewrite successfully without breaking anything, how would we make the total URL something cleaner kurtiskronk.com/weddings/9698 since the $num is the only thing that will change within a given section?
Kurtis, thanks for the extra information. It's a lot easier to give a specific answer to this.
My first comment is that you need to separate out in your thinking URI space -- that is what URIs you want your users to type into their browser -- and filesystem space -- what physical files you want to map to. Some of your mappings are URI->URI and some are URI->FS
For example you want to issue a permanent redirect of www.kurtiskronk.com/* to kurtiskronk.com/*. Assuming that you only server the base and www subdomains from this tree, then this cond/rule pair should come first, so that you can assume that all other rules only refer to kurtiskronk.com.
Next, you need to review the RewiteBase documentation. .htaccess files are processed in what Apache calls a Per-Directory context and this directive tells the rewrite engine what to assume as the URI base which got to this directory and .htaccess file. From what I gather, your blog is installed in docroot/blog (in the filesystem, and that you want to get to directory by typing in http://kurtiskronk.com/blog/ but that this .htaccess file is for the root folder -- that is the base should be (this goes before the www mapping rule)
DirectorySlash On
DirectoryIndex index.php
RewriteBase /
#301 redirect to domain without 'www.'
RewriteCond %{HTTP_HOST} ^www\.kurtiskronk\.com$ [NC]
RewriteRule ^(.*)$ http://kurtiskronk.com/$1 [R=301,NC]
You can add some field dumps look for REDIRECT_* in the Server or Environment table in the phpinfo O/P to see if these are sensible. For example:
RewriteWrite ^(.*)$ - \
[E=TESTDR:%{DOCUMENT_ROOT}/pages/$1.php,E=TESTPDR:%{DOCUMENT_ROOT}/pages/$1.php]
Your next rule is that if the file exists in the subdirectory pages then use it:
RewriteCond %{DOCUMENT_ROOT}/pages/$1 -f
RewriteRule ^(.*)$ pages/$1 [NS,L]
[Note that some shared service sites don't set up DOCUMENT_ROOT properly for the rewrite engine so you may need to run a variableinfo script (<?php phpinfo(INFO_ENVIRONMENT | INFO_VARIABLES); to see if it sets up alternatives. On your site you have to use %{ENV:PHP_DOCUMENT_ROOT} instead.]
Your next rule is that if the file exists, but with the extension .php in the subdirectory pages then use it:
RewriteCond %{DOCUMENT_ROOT}/pages/$1.php -f
RewriteRule ^(.*)$ pages/$1.php [NS,L]
Now redirect any blog references to the blog subdirectory unless the URI maps to a real file (e.g. the blog stylesheets and your uploads.)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/ blog/index.php [L]
A complication here is that WP may be using a poorly documented Apache feature call Path Info that is a script can act as a pseudo directory so http://kurtiskronk.com/blog/tag/downtown/ is redirected to docroot/blog/index.php/tag/downtown/ which is then executed by `docroot/blog/index.php using /tag/downtown/ as the PATH_INFO. But this is one for Wordpress experts to comment on. If this last rule doesn't work then try:
RewriteRule ^blog/(.*) blog/index.php/$1 [L]
PS. I like your site. I wish I was that young again :(
Postscript
When you say "it doesn't work", what doesn't with this .htaccess?
http://kurtiskronk.com/phpinfo,
http://kurtiskronk.com/phpinfo.php,
http://kurtiskronk.comblog/tag/downtown/
It's just that these rules work for these tests (with domain swapped) on mine. (One way is to move or copy the above variableinfo.php to the various subdirectories. If necessary temporarily rename the index.php to index.php.keep, say, and copy the variableinfo.php to the index.php file. You can now enter the various URI test patterns and see what is happening. Look for the REDIRECT_* fields in the phpinfo output, and the SCRIPT_NAME will tell you which is being executed. You can add more {E=...] flags to examine the various pattern results. (Remember that these only get assigned if the rule is a match.
Lastly note the changes above especially the additional NS flags. For some reason mod_rewrite was going directly into a subquery which was resulting in redirect: being dumped into the file pattern. I've had a look at the Apache code and this is a internal botch to flag that further redirection needs to take place (which then replaces this or backs out). However this open bug indicates that this backout can be missed in sub-queries and maybe that's what is happening here. Certainly adding the NS flas cured the problem on my test environment.
PS. Note the added explicit DirectoryIndex directive and also that whilst http://kurtiskronk.com will run the root index.php, the explicit /index.php version will run the one in pages, because that's what your rules say.
Here is a simple solution. You can use it apache conf file(s) or in .htaccess (easier to set up when you're trying).
mod_rewrite has to be enabled.
For example, use .htaccess in your DocumentRoot with:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /pages/$1.php [L]
It will redirect /about to /pages/about.php, and any other page.
The "RewriteCond" part is to authorize access to an existing file (eg: if you had an "about" file at the root of your site, then it will be served, instead of redirecting to /pages/about.php).
Options +FollowSymLinks
RewriteEngine On
RewriteRule /([0-9]+)$ /pages/$1.php [L]
Put something like this in your .htaccess file. I guess that is what you want.
Juest a redirect from a simple url to a longer url.
I am playing with mod_rewrite now, and have successfully enabled it.
However, I need to put a htaccess file inside var/www/ in order to achieve what I want, which is to rename Urls simply...
When I place it my website becomes very strange and nothing basically works...
Is there any code I need to put into the htaccess file in order for things to act normally?
Here is the htaccess file I have so far:
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/ad\.php
RewriteRule ^(.*)$ ad.php?ad_id=$1 [L]
My DocumentRoot is also set to var/www/ and my entire website root is there... (index.html etc etc)...
What am I missing about the htaccess?
If you need more input let me know...
I suspect that none of your css files, js or images are loaded. Furthermore, none of your links work either. If so, the problem could be in the RewriteRule cause basically that rule is telling apache to pass all requests to ad.php
You need to fine tune your RewriteRule, so that only the ad links are being affected by the rule.
First, by expanding the RewriteRule like this:
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_URI} !^/ad\.php
RewriteRule ^(.*)$ ad.php?ad_id=$1 [L]
These 3 lines that I've added are telling apache to apply the rule only if the requested filename is not a directory, an existing file or a symbolic link - this should take care of the static content, such as the css and images. If your other pages where you're links are pointing at, are also physically on the HDD of the server (plain html or php files), should start working again.
But, as I already said on this question of yours (Little mod_rewrite problem) you need to fine tune that rule, so that only ads are being met by the rule and nothing else.
This isn't a complete answer but it will give your more information that can help you.
You can put options on the same line Options +FollowSymLinks -Indexes
Not all hosts allow .htaccess, or not all .htaccess commands. This can cause problems and pages not to work.
Try commenting out everything (using # sign in front of each line), then, starting at the top, uncomment until you find the problem line.