.htaccess won't redirect without arguments - .htaccess

My .htaccess won't redirect a URL correctly for some strange reason, whenever I attempt direct traffic from http://example.com/b70a0 to then into http://example.com/serve/?short=b70a0 I get a 404 page as if the page was not found, when of course there was never going to be a page there in the first place, I wanted to redirect the traffic from that page.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ serve.php?short=$1 [QSA,L]

I found promptly the answer to my issue, turns out I needed to have "AllowOverride" to be allowed inside of my actual Apache settings, as that can be found by heading into the
/etc/apache2/sites-available/
folder, the following is what I found to be useful when making this resource.
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
</Directory>

Related

Apache not redirecting to index.php properly

I have an application and the entry point is index.php in /var/www/html/quote directory. The application is using slim framework to deliver json api. So I tested the application using php -S localhost:8080 but now I'm ready to upload to my hosting service so I want to be able to test the app by typing localhost/quote. I enabled mod_rewrite in apache2. Also I added this to the directory where index.php is localted
.htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
finally, I modified the apache2.conf file and change directive like this. changing AllowOverride to All
<Directory /var/www>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Now when I type http://localhost/quote. I'm getting "page not found"
what am I missing?

Simple .htaccess rewrite rule doesn't work

I have the following .htaccess file. It refuses to access the following url structure:
www.example.com/test
Even though it accesses this fine:
www.example.com/test.php
I've ran some more complicated rewrite rules using this file and it worked just fine. For example:
RewriteRule ^tests/([0-9]+)/?$ /tests_page.php?id=$1 [PT,L,QSA]
I don't understand how this can be happening. What am I missing here?
#OPTIONS
Options -Indexes
Options +FollowSymLinks
#ACCESS TO THE .HTACCESS FILE
<Files .htaccess>
order allow,deny
deny from all
</Files>
#REWRITE ENGINE
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteEngine On
#PAGES REWRITE
RewriteRule ^test/?$ /test.php
Update:
I renamed the file to tests.php and changed the rule to this and it worked:
RewriteRule ^test/?$ /tests.php
This still does not explain why this is happening though.
Why can't I have the url folder name match the file name?
This appears to be a problem with MultiViews option. Disable it by using:
Options -MultiViews
line at the top of your .htaccess. Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite module and makes Apache server match extensions of files. So /file can be in URI but it will serve /file.php.

Basis mod_rewrite rule results in 500 internal server error

I have this very basic rewrite rule, no matter what I try, results in an Error 500.
RewriteEngine On
RewriteRule ^folder/(.*) /folder/index.php?Alias=$1 [L]
My httpd.conf file has the following content: (which seems OK to me)
<Directory "/var/www/html">
Options -Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
<IfModule mod_suphp.c>
suPHP_Engine On
suPHP_UserGroup webapps webapps
SetEnv PHP_INI_SCAN_DIR
</IfModule>
</Directory>
Any suggestions on what might be going wrong? I've also tried to add $ at the end of my rewrite rule.
The rewrite engine will loop repeatedly, until the URI stops changing, or the internal redirect limit is reached which causes the 500 error to be thrown. Your rule's target URI /folder/index.php will get thrown back into the rewrite engine and your same rule's regex matches it, ^folder/(.*). So you'll need to add some kind of condition to prevent the loop.
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/folder/index\.php
RewriteRule ^folder/(.*) /folder/index.php?Alias=$1 [L]
This is simple, it simply won't apply the rule if it already starts with /folder/index\.php. You can also try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^folder/(.*) /folder/index.php?Alias=$1 [L]
This is a little less restrictive of a condition. It only applies the rule if the requested URI doesn't map to an existing file or directory. This assumes that when you try to go to /folder/blahblah there isn't a directory or file blahblah and that you want to route it through your index.php.

htaccess redirect all content including subdomains

I have 3 domains, for the same web app, since I don't want to have duplicated content I want to redirect the other two domains to the primary domain.
If a user types client_xyz.domain2.com it should be redirected to client_xyz.primarydomain.com.
If a user types client_xyz.domain2.com/folder/file/etc it should be redirected to client_xyz.primarydomain.com/folder/file/etc.
If a user types domain2.com/test/page it should be redirected to primarydomain.com/test/page
I thought this is the best solution to avoid Google penalty for duplicated content.
If you think there's a better solution to deal with this (eg. DNS), let me know.
Put this code in your .htaccess file:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteOptions MaxRedirects=10
RewriteCond %{HTTP_HOST} ^(client_xyz\.)?domain2\.com$ [NC]
RewriteRule ^ http://%1primarydomain.com%{REQUEST_URI} [NE,R=301,L]
You can user regex in the .htaccess file, so set up a 301 (moved permanently) redirect.
RedirectMatch 301 (^.*?\.|^)domain2.com(.*) $1.primarydomain.com$2
Untested, but should be fine.
[edit]The below is fairly useless since your comment, but I'll leave it here anyway.
Alternatively, you could set up server alias's in your Apache config file. You probably have something like this:
ServerName primarydomain.com
DocumentRoot /var/www/html/mysite/
<Directory "/var/www/html/mysite/">
AllowOverride All
</Directory>
Change it to:
ServerName primarydomain.com
ServerAlias domain2.com
DocumentRoot /var/www/html/mysite/
<Directory "/var/www/html/mysite/">
AllowOverride All
</Directory>
If you can use mod_rewrite, this should work:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} (^.*?\.|^)?domain2\.com
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([a-z0-9-]+)/? http://$1.primarydomain.com [R=301,NC,L]
If not, well, I'm probably going to have to leave it up to someone smarter than me ;-)

.htaccess equivalent of baseurl?

I'm trying to install Symfony on a shared server and am attempting to duplicate the httpd.conf command:
# Be sure to only have this line once in your configuration
NameVirtualHost 127.0.0.1:8080
# This is the configuration for your project
Listen 127.0.0.1:8080
<VirtualHost 127.0.0.1:8080>
DocumentRoot "/home/sfproject/web"
DirectoryIndex index.php
<Directory "/home/sfproject/web">
AllowOverride All
Allow from All
</Directory>
Alias /sf /home/sfproject/lib/vendor/symfony/data/web/sf
<Directory "/home/sfproject/lib/vendor/symfony/data/web/sf">
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
I need to do so using .htaccess
The redirect portion was done in the root using the following:
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^symfony.mysite.ca [nc]
rewriterule ^(.*)$ http://symfony.mysite.ca/web/$1 [r=301,nc]
For the alias, I've tried using:
RewriteBase /
but no success.
The main issue is that the index.php file residing in the /web folder uses the /web path in its path to images and scripts. So instead of
href="/css/main.css" //this would work
it uses
href="/web/css/main.css" //this doesn't work, already in the /web/ directory!
Any ideas? Thanks!
You can use...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
to prevent urls that refer to actual files from being rewritten, thus preventing the generic rewrite from adding a second /web into the URL.

Resources