I've looked through many questions on here but haven't found one that quite answers my question (could be wrong, feel free to suggest an answer I missed)
How would you write the .htaccess file to send all requests from the folder
example.com/subpage/img to example.com/img and example.com/subpage/js to example.com/js
Where subpage can be any text string
This is the file so far
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule (^[A-Za-z]+[\/]?) index.php?page=$1 [L]
Secondly, how could you add a second parameter to the last rule? like index.php?page=$1subpage=$2
Please let me know if this needs to be more specific
Add this rule to your existing code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^[^/]+/((?:img|js)/.*)$ /$1 [L,NC]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([a-z]+)(?:/([a-z]+))?/?$ index.php?page=$1&subpage=$2 [L,NC,QSA]
Related
I'm in the process of trying to prettify my URLs
From: http://localhost/blog.php?id=1
To: http://localhost/blog/1
I've added the below to my .htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^blog/([a-zA-Z0-9]+)$ blog.php?id=$1 [NC,L]
From my understanding that should now redirect when I call my "to" url in PHP header("location: /blog/".id); it takes me to the relevant page, and my $_GET['id'] should have a value. It's coming up empty
I've been messing around with a few different regular expressions, but I don't think that's the issue.
In the apache config I changed "AllowOverride" to 'All'
To check to see if the .htaccess was even being used I put in some invalid regex, and got an error in apache_error.log
I've been on a ton of different pages asking the same thing, and watched plenty of YouTube vids, but I can't see what I'm missing
Have your htaccess Rules file in following manner. Please make sure to clear your browser cache before testing your URLs.
Options +FollowSymLinks -MultiViews
RewriteEngine On
##Rules for external redirect to blog/1 here.
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s/(blog)\.php\?id=(\d+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
##Rules for internal rewrite to blog.php file here.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^blog/([\w-]+)/?$ blog.php?id=$1 [QSA,NC,L]
##Rules for internal rewrite to index.php file here.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/?$ index.php?id=$1 [QSA,NC,L]
try this .
1.
Options +FollowSymLinks
RewriteEngine on
RewriteRule blog/id/(.*)/ blog.php?id=$1
RewriteRule blog/id/(.*) blog.php?id=$1
example url :- http://localhost/blog/id/1/
Directory Type URL
I'm trying to rewrite URLs using htaccess. The idea behind this is quite simple:
Once a visitor visits https://example.com/pineapples, the browser should load https://example.com/index.php?page=pineapples instead while remaining the original URL.
This should only happen when a file exists in /pages/ with the name of the requested URL, in this case pineapples.php.
This also counts for subdirectories: https://example.com/pineapples/flamingos should redirect to https://example.com/index.php?page=pineapples/flamingos when the file exists in /pages/, in this case /pages/pineapples/flamingos.php, where pineapples is a subdirectory.
Now I've tried to do this, but it's very buggy. It doesn't work for subdirectories as explained above, and it seems to work for all the directories, instead of just /pages/.
Options -Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(css|js|jpg|png|gif|svg)$ [NC]
RewriteCond %{REQUEST_URI} !^/pages(/|$)
RewriteRule ^([^/]+/[^/]+/). /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [L,QSA]
If someone please can help me out get this to work, I will be very grateful!
This question has been answered using the following htaccess.
Options -Indexes +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(css|js|jpg|png|gif|svg)$
RewriteRule ^(ajax|assets|auth)($|/) - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
You can't check REQUEST_FILENAME because in your case it has to be in pages
so you may want to try
Options -Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteLog /path/to/log
RewriteLogLevel 5
RewriteCond %{REQUEST_URI} !\.(css|js|jpg|png|gif|svg)$ [NC]
RewriteCond %{REQUEST_URI} !^/pages(/|$)
RewriteRule ^([^/]+/[^/]+/). /$1 [R=301,L]
RewriteCond %{DOCUMENT_ROOT}pages%{REQUEST_URI}.php -f
RewriteRule ^(.+)$ index.php?page=$1 [L,QSA]
Also I have enabled rewrite log, so in case it doesn't work, please post the logs your get
I'm in need of assistance with mod_rewrite. Currently, I've a folder at www.domain.com/folder. I need to leave the files where they are, but have the address bar in the browser appear as someothername.domain.com
Any help would be greatly appreciated!
I should mention that I've already got a few .htaccess rules in place, as shown here:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.+)$ index.php?$1 [QSA,L]
Thanks in advance,
-Ray
You can have 2 .htaccess like this:
DocumentRoot/.htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^((?!folder/).*)$ folder/$1 [L,NC]
/folder/.htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /folder/
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.+)$ index.php?$1 [QSA,L]
hi guys i am trying to rewrite a url but getting errors
i would like to rewrite
www.example.com/photos.php?u=username
to
www.example.com/#username/photos
here is my code
IndexIgnore *
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^#([A-Za-z0-9]+)$ user.php?u=$1 [NC,L]
RewriteRule ^#([A-Za-z0-9]+)$/^([A-Za-z0-9]+)$ user.php?u=$1 photos.php?u=$1 [NC,L]
Your first RewriteRule looks fine, but your second RewriteRule is full of errors.
You're trying to feed the right hand of the rule with 2 different address as seen at:
user.php?u=$1 photos.php?u=$1
Your rule on the left hand have a terminator sign at the middle of the regex and after it you start a new regex which would not work as well:
^#([A-Za-z0-9]+)$/^([A-Za-z0-9]+)$
This should work for your needs and it should be placed inside your .htaccess file, which should be located inside your domain ROOT folder along with both the photos.php and the user.php files or it will not work.
IndexIgnore *
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^#([a-zA-Z0-9]+)/photos$ photos.php?u=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^#([a-zA-Z0-9]+)$ user.php?u=$1 [QSA,L]
If you have a different folder setup then update your question with more information on it so I can update the rule to fit it.
The above rules will allow you to access the photo page in the following format:
www.example.com/#username/photos
And the users page in the following format:
www.example.com/#username
Here are my current URL:
?p=new-homes-by-east-homes&id=416&name=east-homes
Here is how I want to have:
?p=new-homes-by-east-homes&id=416
So far, I have generated this way using .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^p=new-homes-by-([^/]+)&id=([0-9]+)?$ ?p=new-homes-by-place&id=$2&name=$1 [L]
This actually works somehow, however, I miss the question mark on front of the new URL ?p=new....
When I put the question mark, it jump me to the homepage.
You can't match against the query string in the pattern of a RewriteRule. You need to match against the %{QUERY_STRING} var in a RewriteCond then use the % backreferences:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{QUERY_STRING} ^p=new-homes-by-([^/]+)&id=([0-9]+)?$
RewriteRule ^/?$ /?p=new-homes-by-%1&id=%2&name=%1 [L]