Static text on rewriterule - apache

I've been trying different ways to create the url like this:
http://www.domain.com/postal-zip/123456
I've tried:
RewriteRule ^postal-zip/([0-9]+)$ code.php?thecode=$1 [L]
and
RewriteRule ^postal-zip/([0-9]+)$ postal-zip/code.php?thecode=$1 [L]
and a few more things but I have not got the result, does anyone have an idea about this?

Your rules look okay. It sounds like mod-rewrite is not turned on.
First, before your rules, make sure you have:
Options +FollowSymLinks -MultiViews
RewriteEngine On
Next, for this to work, mod_rewrite must both be installed and activated for .htaccess files. If you are not sure, to check if mod_rewrite is even installed, look at the list of installed modules in the output of phpinfo(); By default, mod_rewrite is not enabled for .htaccess files. If you are managing your own server, open httpd.conf and make sure that the webroot directory block contains one of these lines: AllowOverride FileInfo or AllowOverride All

Related

mod_rewrite all to index.php in one environment

I know there are a million of questions about this, but I've tried the solutions in other questions and haven't got it to work in my case. I'm trying to redirect everything to index.php:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
This works in my local environment, but in my staging environment it fails and I get a 404 error. I've checked that mod_rewrite is enabled with phpinfo(). I've tried changing a few things like using ^(.*)$ instead of ^ and using /index.php instead of index.php
What else could be the problem?
Figured it out. It was a problem with the configuration of apache. Whoever set it up for the site I'm working on did it like this in the site's .conf file:
<Directory "/data/path/to/directory">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
# and a bunch of rewrite rules...
</Directory>
The AllowOverride None prohibits the use of .htaccess. In our local and development environments, the directory structure is different, so this code doesn't even get applied. That's why the .htaccess was working.
To solve the problem I'll either have to change the line mentioned above to AllowOverride All or add another configuration for the website I'm working on like the one above that does what I wanted to do with .htaccess.
Problems like this shouldn't come up for anyone if their environments are configured appropriately, but if anyone does run into a similar situation, check the configuration of the site in apache!

How To Use Htacess for www.example.com/type.php?user=name

I want to redirect www.example.com/type/name to www.example.com/type.php?user=name
{
RewriteRule ^([a-zA-Z0-9_-]+)/type$ type.php?key=$1
RewriteRule ^([a-zA-Z0-9_-]+)/type/$ type.php?key=$1
}
Please help me about this.
Try adding this to the .htaccess file in your web document root folder (often public_html or htdocs):
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^type/([^/]+)/? type.php?key=$1 [L]
This assumes that mod_rewrite is both installed and activated for .htaccess files. If you are not sure, to check if mod_rewrite is even installed, look at the list of installed modules in the output of phpinfo(); By default, mod_rewrite is not enabled for .htaccess files. If you are managing your own server, open httpd.conf and make sure that the webroot directory block contains one of these lines: AllowOverride FileInfo or AllowOverride All
try this , hope it helps :
RewriteBase /www.example.com
RewriteEngine On
RewriteRule ^type/(.*)/$ /type.php?user=$1
#OR
RewriteRule ^(.*)/(.*)/$ /$1.php?user=$2
PS : The first one works only for the url : www.example.com/type/value ,the second one works for any page in your site ex: www.example.com/anypage/value

This mod rewrite not working correctly?

I have a url like this
http://example.com/view.php?id=a1b2c3
I want it to look like this
http://example.com/v/a1b2c3
I am trying this
RewriteRule ^v/?$ view.php?id=$1 [NC,L]
But it is not working and I have no idea why
Try this tweak:
RewriteEngine On
RewriteRule ^v/([^/]+)/?$ view.php?id=$1 [NE,L]
This assumes that mod_rewrite is both installed and activated for htaccess files.
If you are not sure, to check if mod_rewrite is installed, look at the list of installed modules in the output of phpinfo();
By default, mod_rewrite is not enabled for htaccess files. If you are managing your own server, open httpd.conf
and make sure that the webroot directory block contains one of these lines: AllowOverride FileInfo or AllowOverride All

mod_rewrite URI with same name as folder or files

A simple .htaccess file with this:
RewriteEngine On
RewriteBase /
RewriteRule ^webshop$ /index.php?page=webshop [L]
... does not work for me because a have a file called webshop.php in the web root. Renaming the file solves the poblem, and changing the regex in the .htaccess file solves the problem, but still - it's only a partial match of the file name...? The only thing I can find on this is to use
DirectorySlash off
I've tried that and it made no difference.
Need some help here, there must be a pretty simple solution to this.
Most likely you have MultiViews options enabled. Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.
Disable it by placing this line on top of your .htaccess:
Options -MultiViews

Url masking through htaccess not working

I was trying to setup Lithium Php framework. I followed the documentation for getting started.
I have done this: *"make sure mod_rewrite is enabled, and the AllowOverride directive is set to 'All' on the necessary directories involved. Be sure to restart the server before checking things."*
Rewrite module is enabled. Verified by
apache2ctl -M
I have changed the AllowOverride in /etc/apache2/sites-available/default file.
Now when I go to http://localhost/LithiumTestApp/, the page loads but sans css, js, images etc as the links do not work.
I can't seem to find what I have done wrong.
I'm running Apache2 on Ubuntu 11.10.
Edit: Contents of .htaccess are:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
Part of Lithium Framework itself. I haven't changed anything here.
I'd recommend first checking to see if your rewrite is working properly.
I usually set a rewrite log path in my vhost file (or set it in your httpd.conf or apache2.conf, whichever your OS uses).
RewriteLog "/var/log/apache2/rewrite.log"
RewriteLogLevel 5
Then do:
tail -f /var/log/apache2/rewrite.log
That will allow you to see the log contents while it's happening and refresh the page and see if everything is in order. I also recommend having a look at your Apache error log to see if there are errors in the request.
Also, make sure your resources folder has write permission.