I'm trying to build a URL rewrite by adding an additional directory in the URL path; for example, changing;
https://FQDN/mypage/ to https://FQDN/dev/mypage/
https://FQDN/template/ to https://FQDN/dev/template/
I have written the following .htaccess although this is having no affect. I have already enabled a2enmod rewrite.
RewriteEngine on
RewriteRule ^dev/(.+)$ /$1
Thank you #anubhava - simple fix, the rewrite was not activating due to AllowOverride None being active in the apache2.conf file. Changing this to AllowOverride All allowed the .htaccess file to be active.
Related
I know there are a million posts about this, but I have read through a lot of them and made so many changes to get this to work, but I am at a loss. I have setup a redirect rule in my apache2 server web server, but it does not redirect.
My web server root folder structure:
/var/www/example/
|
|----l
|
|---index.php
|---.htaccess
/var/www/example/l/.htaccess content is:
$ cat /var/www/example/l/.htaccess
RewriteEngine On
RewriteRule ^l/([a-zA-Z0-9]{6})$ l/index.php?redirect=$1 [R=302,L]
I have also tried putting it in the root folder /var/www/example/.htaccess
My rewrite rule appears to be working correctly from this site:
https://htaccess.madewithlove.be?share=6cae684f-740d-4add-b711-53cdb5986681
mod_rewrite.so is installed and rewrite_module is loaded.
I ran sudo a2enmod rewrite and it was turned on
APache2.conf:
$ cat /etc/apache2/apache2.conf
<Directory "/var/www/example">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
When ever I go to https://example.com/l/i4Ikn2 I just get a 404 error.
I just want to know if I am missing something or somewhere to look. I checked the apache2 error.log and there are no error in there pertaining to this.
As #mrwhite and #anubhava mentioned in the comments below my OP, I was mixing up the way my .htaccess file was being used.
I removed the sub-directory flags (the l in the pattern) and added the QSA instead of the redirect (R=302).
This was because I was using the .htaccess file in the sub-directory.
The QSA was the correct 'option' for the redirect/rewrite (still unsure of the difference).
Original:
RewriteRule ^l/([a-zA-Z0-9]{6})$ l/index.php?redirect=$1 [R=302,L]
New (working):
RewriteRule ^([a-zA-Z0-9]{6})$ index.php?redirect=$1 [QSA,L]
In my apache configuration I have a virtual host configured like this:
Alias /mediamanager /storage/files/mediamanager
<Directory /storage/files/mediamanager>
DirectoryIndex /mediaManagerIndex.php
DAV On
# ... And some authentication directives ... #
</Directory>
The idea is that someone can access the files both by a WebDAV-Client and also a simple web browser in which case some pretty directory view is generated by a PHP script.
That worked great in Apache 2.2, but recently I upgraded to Apache 2.4 and now it is broken. I highly suspect I I suffer from this bug which is already 2 years old and no fix in sight. The proposed workaround to add:
<Limit PROPFIND>
DirectoryIndex never-encounterable-file-name.html
</Limit>
Does not work for me. Probably because I still want to have a directory index. If I remove my DirectoryIndex altogether WebDAV works again (no index.html or similar files exists in this directory) but of course I loose the ability to use my PHP file as directory index. I tried to specify my DirectoryIndex in a <Limit GET> but this had no effect.
Is there any way to get both DAV and DirectoryIndex to work simultaneously in Apache 2.4 on Debian (if anyhow possible without changing the source code and recompiling)?
In order to fix this, disable directory indexing for the WebDAV site.
In your sites-available/site.conf file add DirectoryIndex disabled to the <Directory> declaration, like so:
<Directory /path/to/my/webdav/dir>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Require all granted
DirectoryIndex disabled
</Directory>
Then just reload Apache and you will no longer have that issue:
sudo service apache2 reload
For me, the following configuration solved both problems:
WebDAV works again
directory indexing, if the user uses a web browser to access the repository
It works by manually implementing the directory-indexing feature with simple rewrite rules, which are applied only for the GET request method.
The following code has to be placed inside the server config or virtual host context in the apache configuration file.
# Turn off (automatic) Directory-Indexing
DirectoryIndex disabled
RewriteEngine On
# Rewrite rules for the root directory
RewriteCond "%{REQUEST_METHOD}" "(GET)"
RewriteRule "^/$" "/index.php" [L]
# Rewrite rules for other sub-directories
RewriteCond "%{REQUEST_METHOD}" "(GET)"
# The following line checks, if the index.php file exists
RewriteCond "%{DOCUMENT_ROOT}/$1/index.php" "-f"
RewriteRule "^/(.*)/$" "/$1/index.php" [L]
Don't forget to reload Apache!
This is the solution I am currently using, located in a .htaccess file at the root of the directory tree used by the WebDav service. In this case I do not use PHP, only html files, but it can be easily adapted:
# Turn off automatic directory indexing
Options -Indexes
DirectoryIndex disabled
# Redirect directory requests to index.html, only for GET requests
RewriteEngine On
RewriteCond %{REQUEST_METHOD} "GET"
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ $1index.html [L]
In order to launch always the requested PHP file, just replace "index.html" on the last line by the PHP file name:
RewriteRule ^(.*)$ $1mediaManagerIndex.php [L]
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
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
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