I am attempting to create a sitemap for my website, that has been uploaded to the website. Whenever I attempt to access it, the .htaccess redirects me to the index.php, which means websites that need use my sitemap gets an error, as it only gets to see the site’s index page.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
The above is my .htaccess. How would I go about not redirecting a certain page, such as sitemap.xml?
As of now, I have attempted to get more aquainted with .htaccess, yet I require a whole lot more of the good old learning process. Once I have solved this, at least I will not have to ask any more questions about .htaccess, as I am learning it at this very moment.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
The last CondPattern should be !-f to prevent files from being rewritten. The first two are for directories and symbolic links respectively.
Related
I have a couple of directives which should redirect the user to the right path:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ user?user=$1 [QSA]
So that writing www.mywebsite.com/myprofile it displays www.mywebsite.com/user?user=myprofile
It works properly, but it redirects to that URL.
What I would like is that my browser displayed www.mywebsite.com/user?user=myprofile (which is an existing folder on my server), but still showed www.mywebsite.com/myprofile on the address bar, which is the way many websites (as far as I know) create your own profile page.
How is it possible?
you could try just as this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ user/?user=$1 [QSA]
or
RewriteRule ^([^/]+)$ user/index.php?user=$1 [QSA]
With the slash added, the URL is not redirected by only rewritten.
I want to add another language to my website (an app written in PHP 7).
I found out, good SEO practices say that every page on my site should be accessible from differend URLs, depending on the language.
Currently my .htaccess looks something like this:
RewriteEngine on
Options +FollowSymLinks
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([a-zA-z0-9-]+)$ $1.php [NC,L]
So when user types in (or clicks a link) http://example.com/contact they get page contact.php (if exists).
What I want to achieve is, to redirect http://example.com/en/contact to the very same file contact.php, but with $_GET argument and still redirecting /contact to contact.php (without this argument). I thought that would be:
... everything from above code sample and then:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^en/([a-zA-z0-9-]+)$ $1.php?lang=en [NC,L]
But it doesn't work. Any ideas why and how to make this work?
Last condition checks that en/file.php exists, which is never the case. That's why the rule is never met. Either you remove it (but it will be applied even on nonexistent files) or you use this workaround by rewriting the faulty condition
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^en/([^/]+)$ /$1.php?lang=en [NC,L]
To be more complete, you can also redirect users that try to access /contact.php?lang=en directly (better for SEO). Here is how your final htaccess should look like
RewriteEngine On
# if url is /file.php?lang=en and file exists then redirect to /en/file
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} \s/([^/\s\?&]+)\.php\?lang=en\s [NC]
RewriteRule ^ /en/%1? [R=301,L]
# if url is /en/file and /file.php exists then internally rewrite to /file.php?lang=en
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^en/([^/]+)$ /$1.php?lang=en [NC,L]
Note: the above code is specific to en language, but you can easily adapt it to multiple languages
I've read dozens of posts about similar problems, but I can't get this one figured out.
I have SEO friendly URL's on my site that look like this...
http://www.website.com/tequila/bottle-name.html
http://www.website.com/whiskey/bottle-name.html
http://www.website.com/vodka/bottle-name.html
...where bottle-name is the actual name of the bottle.
I am doing a mod_rewrite to a PHP handler to actually deliver the pages. Each handler is located in each directory (tequila, vodka, whiskey).
The first one (tequila) is working, but the other two are not and I can't figure out why.
Here is the mod_rewrite code I have right now...
#Rewrite seo urls
RewriteCond %{REQUEST_URI} !^/tequila/
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ index.php?liquor=$1&liquor-type=tequila [L]
RewriteCond %{REQUEST_URI} !^/whiskey/
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ index.php?liquor=$1&liquor-type=whiskey [L]
RewriteCond %{REQUEST_URI} !^/vodka/
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ index.php?liquor=$1&liquor-type=vodka [L]
What appears to be happening is the whiskey and vodka pages are getting caught and processed by the tequila mod_rewrite (guessing because it is first in the list here).
So, http://www.website.com/vodka/vodka-bottle.html is actually getting rewritten as http://www.website.com/tequila/index.php?liquor=vodka-bottle.html&liquor-type=tequila.
It seems like I haven't setup the RewriteCond %{REQUEST_URI} !^/tequila/ correctly?
Any help or pointers anyone can provide are greatly appreciated!
Why are you negating the ^/tequila/? The ! will cause a match on all other liquor types, rather than the one you list.
I am using the following .htaccess code in my website to redirect all the urls to index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
then I am checking the url and see if the name of the page is available in the database. Then I am redirecting the user the that specific page if it is found like so:
www.mywebsite.com/videos.php?v=Name_of_the_video
www.mywebsite.com/images.php?i=Name_of_the_image
www.mywebsite.com/users.php?u=Name_of_the_user
as you can see I have 3 main types of pages. I want to use .htacces so I can convert those urls like so:
www.mywebsite.com/videos/Name_of_the_video
www.mywebsite.com/images/Name_of_the_image
www.mywebsite.com/users/Name_of_the_user
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/])([^/]+)/(.*) /$1$2.php?$1=$3 [L,QSA]
You'll probably want to put this before your other rules though.
I would like to run multiple sites from one FuelPHP installation. The main driver behind this is the ability to share models and database configuration.
I've seen a couple of suggestions requiring changes to the FuelPHP directory structure, or through the use of modules.
I don't really want to mess with the directory structure.
I don't think I completely understand how modules can help me, but I think the resulting configuration would be more complex than I need.
The solution I've come up with involves a simple change to the .htaccess file located in /public. Whereby I modify the rewrite rule based on the host name. Each site's controllers and views sit in subdirectories of the main structure.
Original .htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Multiple site .htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} main.loc
RewriteRule ^(.*)$ index.php/main/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} admin.loc
RewriteRule ^(.*)$ index.php/admin/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} api.loc
RewriteRule ^(.*)$ index.php/api/$1 [L]
</IfModule>
My question is, is this a viable solution? Do you foresee any pitfalls that I might encounter down the line?
My working solution has been to create three FuelPHP applications side by side, then symlink key directories like core, some packages and obviously the model directory.
One gotcha was that my FTP client ignored the symlinks, so I had to recreate them on the staging server. This will not be an issue when I finally get around to automating deployment using SSH.