I have a challenge using Apache.
In my .htaccess file I'd like to convert requests like this:
url/portfolio/filename.htm
to:
url?filename
Any takers? Thanks for your time
You have a couple of choices, depending on how you want the URL to appear to visitors (and search engines).
If you want the externally visible URL to remain url/portfolio/filename.htm, Alec's solution worked after I removed the two RewriteCond lines.
RewriteEngine On
RewriteRule ^(.*)/portfolio/(.*)\.htm$ $1?$2 [PT,L]
If other parameters could be in the query string and you want to preserve those, add QSA to the options in brackets at the end of the rule.
If you want people outside to see url?filename instead, change the rule to:
RewriteRule ^(.*)/portfolio/(.*)\.htm$ $1?$2 [L,R]
Same qualification about other query parameters applies.
If this still doesn't help, I suggest you turn on rewrite logging and look in that log for more clues. Post them here and someone will help. You might have to put this part in httpd.conf. My Apache didn't like it in .htaccess.
RewriteLog ...path...
RewriteLogLevel 3 # you'll regret anything higher
Something like this should work, although I have not tested it.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)/portfolio/(.*)\.htm$ $1?$2 [PT,L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
This should work:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^portfolio/(.+)\.htm$ index.php?$1 [NC,L]
or if you want to visibly change it:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^portfolio/(.+)\.htm$ /?$1 [R,NC,L]
The second one would look like example.com/?filename to the user, whereas the first one would look like example.com/portfolio/filename.htm.
Hope that helps.
Related
i have following htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteRule !^/?(maintenance\.html|.*\.css|.*\.js)$ http://www.example.com/maintenance.html [R=302,L]
RewriteRule ^([A-Za-z0-9]+)$ $1.php
RewriteRule ^home\.php$ index.php
RewriteRule ^home$ index.php
as you can see all, no matter which page you visit on the website it will be redirected to maintenance.html. I also need a small change that will exclude one more location from this. Since I am using Magento, I need to exclude admin area from this redirect so I think these are the urls that I need
http://www.example.com/index.php/admin_852_in
http://www.example.com/skin/
http://www.example.com/js/
http://www.example.com/media/
What rule should I add for these two urls too?
thanks!
You can tweak your exclusions like this:
Options +FollowSymLinks
RewriteEngine on
RewriteRule !^/?(maintenance\.html$|index\.php/admin|css/|js/|.*\.(css|js)$) http://www.example.com/maintenance.html [R=302,L,NC]
I know this question has been asked and answered many times, but my experience of .htaccess, regualr expressions, mod rewrites etc.. have normally drove me crazy.
I see on most websites the structure of the url is in a directory-like structure, wwww.linku.biz/edit. My ultimate question is how do you do this?
Are all these sites behind the scenes have normal URL variables but just re-wrote? such as www.linku.biz/myprofile?edit="whatever" , is this all done with .htaccess, and mod_rewrites?
I want to type in my url www.linku.biz/search however its actually www.linku.biz/search.php
I want to type in my url www.linku.biz/JackTrow however its actually www.linku.biz/profile.php?us="JackTrow
Also I want data re-wrote when I have lots of URL data, such as www.linku.biz/search?a=1&b=2&c=3 actually beeing www.linku.biz/search.php?a=1&b=2&c=3
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+search\.php\[?^\s] [NC]
RewriteRule ^ search? [R=301,L]
RewriteRule ^search/?$ search.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ profile.php?us=$1 [QSA,L]
Please note that your requirement 1 & 3 will both be covered by rule # 1
Add this rules to your .htaccess file in Root folder :
To do this www.linku.biz/JackTrow <--- www.linku.biz/profile.php?us="JackTrow
RewriteRule ^profile.php?us=(.*)$ /$1 [R=301,L]
To do this : www.linku.biz/search however <--- www.linku.biz/search.php
RewriteRule ^(.*).php$ /$1 [R=301,L]
Your last one , i have not understood what you want exactly.
I don't work to much with mod_rewrite so I could easily be just missing something simple. I want to match and replace a specific URL. The URL I want to rewrite would look like this:
http://www.sample.com/hello to http://www.sample.com/index.php?page=hello
My htaccess currently has the following rule in place:
RewriteRule ^(hello)/?$ index.php?page=$1 [L]
However, when I implement the rule and I test it by trying to go to http://www.sample.com/hello or http://www.sample.com/hello/ I always get my 404 error document. ugh I feel like it should be simple and I must be missing something right there. Any help would be fantastic and much appreciated!
Here is my whole file for reference:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?sample\.com/ [NC]
RewriteCond %{REQUEST_URI} !hotlink\.(gif|png) [NC]
RewriteRule ^(.*)\.(gif|jpe?g|png)$ sample.com/hotlink.png[R,NC,L]
RewriteCond %{HTTP_HOST} ^www.sample\.com$ [NC]
RewriteRule ^(.*)$ http://sample.com/$1 [R=301,L]
RewriteRule ^(hello)/?$ index.php?page=$1 [L]
Thanks
Your code works perfeclty fine for me. Maybe you're missing
RewriteEngine On
RewriteRule ^(hello)/?$ page.php?page=$1 [L]
or maybe mod_rewrite is not installed or loaded at all.
Look for
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
in your main apache configuration file httpd.conf
This is one of those times...
I cleared the htaccess file and only used the following couple lines and tested it out:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
#
RewriteRule ^(hello)/?$ page.php?page=$1 [L]
I realized looking at the non custom 404 page that it referenced page.php and not index.php!
Not Found
The requested URL /page.php was not found on this server.
So turns out with all my edits I mistakenly typed page.php in the rule instead of index.php. I corrected it and the rule works. One of those times :)
Thanks all!
I'm having some problems in my .htaccess file.
I would like to display the content of this URL:
http://mywebsite.com/admin/payments/invoices/view/index.php?id=123456
When I access this one:
http://mywebsite.com/admin/payments/invoices/view/123456/
Here's my actual .htaccess
Options +FollowSymlinks
RewriteEngine On
RewriteBase /admin/payments/invoices/
RewriteRule ^/view/([0-9]+)/(index.php)?$ /view/index.php?id=$1 [L,QSA]
Do you have any idea?
Thanks!
If you do have the view directory, then the easiest thing is to put this .htaccess in that directory (i.e. {DOCUMENT_ROOT}/admin/payments/invoices/view/.htaccess):
Options +FollowSymlinks
RewriteEngine On
RewriteBase /admin/payments/invoices/view
RewriteRule ^(\d+)$ index.php?id=$1 [L,QSA]
The index.php in the left side hand of the RewriteRule is not required (actually, I expect it not to work: the DirectoryIndex should not yet have been injected in the URI at this stage - unless some other RewriteRule is in effect?), nor is the / at the end of the RewriteBase.
I tested the above on Apache/2.2.21, but the module rules are the same for later versions.
Try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^.*admin/payments/invoices/view/index\.php\?id=([0-9]+) admin/payments/invoices/view/$1/index.php [L]
</IfModule>
Try putting this in the htaccess file in your document root:
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)id=([0-9]+)
RewriteRule ^admin/payments/invoices/view/index\.php$ /admin/payments/invoices/view/%1/index.php [L]
So when you enter http://mywebsite.com/admin/payments/invoices/view/index.php?id=123456 in your browser's URL address bar, you will get served the content at /admin/payments/invoices/view/123456/index.php. Since it's completely unclear what you actually want, in case you wanted it the other way around:
RewriteEngine On
RewriteRule ^admin/payments/invoices/view/([0-9]+)/(index.php)?$ /admin/payments/invoices/view/index.php?id=$1 [L,QSA]
I've got the following in .htaccess:
RewriteCond %{REQUEST_URI} !app/index\.php$
RewriteRule ^(.*\.htm)$ index.php?url=$0 [QSA,L]
Everyting works great but result URLs have empty $_POST.
I'm 100% sure that it's mod_rewrite bug, not HTML or PHP because when I change [QSA,L] to [L] it works great. But I really need QSA.
It happens at about 30% hostings so this may be some Apache config option.
What about modifying your rule in this way. Does it change anything?
RewriteRule ^(.*)\.htm$ index.php?url=$0.htm [QSA,L]
by default,.. maybe apache not enable mod_rewrite, try type this on your console
sudo a2enmod rewrite
sample my .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
I had exactly this issue. It works on one server then not the next. Annoying and time consuming to say the least!
After a lot of checking using this php:
foreach ($_REQUEST AS $key => $value) echo $value."<br>";
at the top of the index.php to check what values were being sent from the htaccess file, (and many other checks I will not go into!) I eventually found you need to specify "RewriteBase"
Adding RewriteBase / to the top of the htaccess file made it work on the all the servers I tried.
(also, remember to set to: RewriteBase /folder-where-file-is/
where "folder-where-file-is" is the location of the index.php file if in a sub-folder)
Hope it helps you - knowing this would certainly have helped me many many hours ago!