I have a gallery script that works very simply.
The index page (php) loads all pictures in thumbnail view based on the url params. I'm trying to use the rewrite rules to mask the urls so they look nicer, but I'm having an issue.
The index page takes a=gallery&name={name of gallery}
The rewrite is working to the extent that it's rewriting the urls to /gallery/{name of gallery}
The problem is I'm getting /gallery/{name of gallery}/?a=gallery&name={name of gallery} which is obviously not desired and redundant anyway.
Here's my rule, I've been messing with this and I'm tired to going over the docs and just want this finished.
# enables rewrite engine
RewriteEngine on
# we always direct requests to the www subdomain
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# some url rewriting
RewriteRule ^([^/\.]+)$ ?a=$1 [L]
RewriteRule ^([^/\.]+)/$ ?a=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)$ ?a=$1&name=$2 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/$ ?a=$1&name=$2 [L]
Edit: added complete htaccess file...
Try it this way and see how it works for you.
# enables rewrite engine
RewriteEngine on
# we always direct requests to the www subdomain
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/gallery
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+) index.php?url=$1&name=$2 [PT,L]
RewriteCond %{REQUEST_URI} ^/gallery
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+) index.php?url=$1 [PT,L]
Related
I have a htaccess that rewrites to /login if conditions aren't met.
It's working fine for urls without query_string.
However I have no clue how to include /reset?query_string to accepted conditions. And want to exclude /reset
/reset?6tdsBMxpeeJEDUvYzwKmLzIusLYLjgMtIj
RewriteEngine On
RewriteBase /index.php
RewriteCond %{REQUEST_URI} !^/login$
RewriteCond %{REQUEST_URI} !^/forgotten$
RewriteRule ^([^\.]+)$ /login [R=301,L,QSD]
RewriteRule ^(/)?$ /login [R=301,L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Here is a modified version of your .htaccess that excludes /reset?query:
RewriteEngine On
# /reset with query string
RewriteCond %{QUERY_STRING} .
RewriteRule ^(reset)/?$ $1.php [L,NC]
RewriteCond %{REQUEST_URI} !^/(forgotten|login)$ [NC]
RewriteRule ^([^.]*)$ /login [R=301,L,QSD]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+)$ $1.php [L]
Make sure to test it after clearing your browser cache.
With your shown samples and attempts, please try following htaccess rules file. Make sure to clear your browser cache before testing your URLs.
Also I am making use of apache's variable named THE_REQUEST by which we can handle both uri as well as query string.
RewriteEngine ON
RewriteBase /
##Rules for handling rest uri without query string.
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^reset/?$ /login? [R=301,L,NC]
##Rules for reset with uri and query string.
RewriteCond %{THE_REQUEST} \s/reset\?\S+\s [NC]
RewriteRule ^ /reset? [R=301,NC]
##Rule for login page's backend rewrite.
RewriteRule ^login/?$ login.php [QSA,NC,L]
##Rule for forgotten page's backend rewrite.
RewriteRule ^forgotten/?$ forgotten.php [QSA,NC,L]
##Rules for non-existing pages should be served with index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
Can someone help me with the htaccess rewrite url?
I need to convert ?lang=en to /en
To always display the language used in the url
When someone comes to example.com to automatically 301 redirect them
to example.com/en as the main language.
When a user chooses another language to stay on the same page e.g.
example.com/en/contact.php
I have a total of three languages on the site en, de, hr.
The problem with my code is when I go to example.com/contact and select a language, it takes me back to the homepage example.com/en. If I type manually example.com/en/contact it works.
If I am at example.com/en/contact and choose another language e.g. /de url changes to example.com/en/de and a 404 error.
My current htaccess code looks like this
# Convert ?lang=en in /en
RewriteRule ^([a-z]{2})/?$ ?lang=$1 [QSA,L]
RewriteRule ^([bs|en|de]{2})/(.*)$ $2?lang=$1&%{QUERY_STRING} [L,QSA]
# Remove trailing slash from URL
RewriteCond %{REQUEST_URI} /(.+)/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ https://example.com/%1 [R=301,L]
# Remove php extension in URL
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
With your shown samples, could you please try following.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-SSL} !ON
RewriteCond %{HTTP_HOST} ^(?:www\.)reg\.bmi\.id$ [NC]
RewriteRule ^/?$ https://reg.bmi.id [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\scontact/contact\.php [NC]
RewriteRule ^ https://reg.bmi.id [R=301]
RewriteRule ^ contact/contact.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Currently my .htaccess looks like this and works perfect for http.
It redirects to www. and removes the .html file extension.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
I have tried the .htaccess from this answer but still the site in question is completely messed up. E.g https://example.com/work shows a 404.
Also all images that are linked in the source code with /img/example-01.jpg do not show.
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Can you please help me with getting a .htaccess file that:
1.
Redirects to https
2.
Redirects to www. subdomain
3.
Removes .html from file extension so that example.com/work shows the work.html page.
Thank you for your help.
If your current .htaccess works fine, this modification should do:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{REQUEST_SCHEME} !https
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
I want to rewrite all requests to index.php?r=
So that /sth becomes /index.php?r=sth
I have applied this rule that works:
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteCond %{REQUEST_URI} !^.*\.(jpg|css|js|gif|png|pdf|php)$ [NC]
RewriteRule ^(.+)$ index.php?r=$1 [L]
However I want to also have redirects such as
Redirect 301 /sth /sth-new
What happens is that it works but the url becomes:
/sth-new?r=sth
Do you have any solutions - suggestions about why this is happening?
How can I implement a global rule and also have simple redirects?
You can do it like this:
RewriteRule ^sth/?$ /sth-new? [L,R=301]
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond %{REQUEST_URI} !\.(jpg|css|js|gif|png|pdf|php)$ [NC]
RewriteRule ^(.+)$ index.php?r=$1 [L]
You should keep external redirect rules before your internal rewrite rules.
I've been trying everything to manage a redirect from www.domain.com to domain.com,
but nothing seems to work for me. I always get a redirect loop - and I've tried various things I found here or on Google.
So here is my .htaccess, maybe someone could help me figure out what I can do to redirect correctly or if there is something wrong in here.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
# Redirect all to .php
# Example: example.com/hello -> example.com/hello.php
RewriteRule ^(.*)$ $1.php [L,R=301]
# show example.com/index.php always as example.com/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://example.com/ [R=301,L]
Thank you so much!
I've already spent so much time trying to figure this out.
You have a rule that always matches, which is responsible for the infinite redirection. I've updated your ruleset below to fix that problem and perform the redirection you mentioned at the top of the answer. Let me know if this does what you expect.
RewriteEngine On
# Redirect www.example.com to example.com
RewriteCond %{HTTP_HOST} ^www [NC]
RewriteRule ^.*$ http://example.com/$0 [R=301,L]
# This performs an external redirection? Is that what you want?
# Don't do the rewrite if we're already pointing at a file, otherwise we'll
# just redirect over and over because .* matches what we redirect to, too
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^.+$ $0.php [L,R=301]
# show example.com/index.php always as example.com/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://example.com/ [R=301,L]
The answer is Apache documentation, the documentation tell how to force usage of www. You just have to reverse the example.
RewriteCond %{HTTP_HOST} !^example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://example.com/$1 [L,R]