I have created a url shortener. This is my .htaccess file:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9]{6})$ index.php?redirect=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php/$1 [L]
When I type example.com/Ran41D I get 404 error, when I type example.com/index.php I can see the filename in address bar.
I expect example.com/Ran41D to forward the url to example.com/index.php?redirect=Ran41D - which is working great, and to see in address bar example.com/ instead of example.com/index.php
Related
for the last hours I cant seem to figure out why my .htaccess file is redirecting to a blank url. Here is my htaccess file below.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^(.*)$ /test/user-profile/%1? [R=301,L]
I am trying to rewrite www.example/test/user-profile?id=4 ->>> www.example/test/user-profile/4
It does rewrite the url, however then the page has "The requested URL was not found on this server." the htaccess file is in public_html folder, so I am trying to select a specific user inside www.exampleurl.com/test/userslist.php which would go to www.exampleurl.com/test/user-profile.php. It all works perfectly before using htaccess. www.example/test/user-profile?id=4 ->>> www.example/test/user-profile/4 but it just wont find the url or file? im a super noob folder path below -> public_html/test/userslist.php & user-profile.php
Just cant seem to figure out what to do. Yes I do have mod-rewrite on.
With your shown samples, please try following htaccess rules file.
This assumes that you are hitting link www.example/test/user-profile?id=4 in browser which is redirecting to www.example/test/user-profile/4 and is being served by index.php with parameters user-profile=user-profile in your url and id=digits in url. You can also change them as per your need.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine On
RewriteBase /test/
RewriteCond %{THE_REQUEST} \s/(test/user-profile)\?id=(\d+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]*/)/([*/]*)/?$ test/user-profile.php?user-profile=$1&id=$2 [QSA,L]
2nd solution: In case someone is hitting url example.com/test/user-profile/7 then try following htaccess rules.
RewriteEngine On
RewriteBase /test/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]*/)/([^/]*)/(\d+)/?$ test/user-profile.php?user-profile=$1&id=$2 [QSA,L]
I am not very familiar with .htaccess, but have the following in my .htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^centurion/programme-(.*).php$ ./centurion/programme.php?cid=$1
RewriteRule ^klerksdorp/programme-(.*).php$ ./klerksdorp/programme.php?cid=$1
RewriteRule ^tygervalley/programme-(.*).php$ ./tygervalley/programme.php?cid=$1
RewriteRule ^rustenburg/programme-(.*).php$ ./rustenburg/programme.php?cid=$1
RewriteRule ^witbank/programme-(.*).php$ ./witbank/programme.php?cid=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
The code above basically make the url's "friendly" by changing the url's to something more descriptive for dynamic pages and removing the .php extentions of the "static" pages.
This all works perfect. But now I need to redirect error/file not found pages as well. I tried adding the following (with relative paths and absolute paths):
ErrorDocument 404 /index.php
But when I enter an incorrect url (in the browser), I still get "file not found" error (not the redirected page).
When I add the following, all the pages redirect to the index.php page:
RewriteRule ^.*$ /index.php [L]
When I have only the following code in my .htaccess file, the incorrect url's redirect as expected.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ /index.php [L]
But then, obviously, non of my "friendly" url rules work.
How can I have both the "friendly" url rules as well as the incorrect url redirects work together?
I have this code in my .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(online-shop)/?$ $1/home [L,NC]
RewriteRule ^(my)/?$ $1/home [L,NC]
RewriteRule ^(blog)/(post|tags)/([\w-]+)/?$ index.php?id=$1&type=$2&unique=$3 [L,QSA,NC]
RewriteRule ^(blog)/(archives)/([0-9]{4})/([0-9]{2})?$ index.php?id=$1&type=$2&year=$3&month=$4 [L,QSA,NC]
RewriteRule ^([\w/-]+)/?$ index.php?id=$1 [L,QSA]
So the following URLs are rewritten:
domain.com/home to domain.com/index.php?id=home
However if i look at a subdomain (cp.domain.com) i get the following error:
The requested URL /home/user/public_html/index.php was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
so the subdomains are looking in the ROOT directory for the domain however they should be looking in the directory set in the control panel
As soon as i remove this line of code:
RewriteRule ^([\w/-]+)/?$ index.php?id=$1 [L,QSA]
the subdomains start working fine
You can try to restrict last rule to only work for main domain:
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w/-]+)/?$ index.php?id=$1 [L,QSA]
I have an hidden folder hidden_folder which can be accessed by the server with
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ hidden_folder/$1
Now, I want to redirect all unavailable pages to 404 error page content.
Illustration 1 : When user tries to go here http://website.com/some_unavailable_uri, the url of the page doesn't change but the content will be the one of /error/404.php
Illustration 2 : http://www.apple.com/test. This page does not exist, the url does not change, but the content displays error.
Basically, it should be if file does not exist, then redirect content. I'd tried something but with no success.
First try :
RewriteEngine On
RewriteCond hidden_folder%{REQUEST_URI} -f # %{REQUEST_URI} already has "/" at its beginning
RewriteRule ^.*$ error/404.php [NC,L]
Second try :
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}hidden_folder%{REQUEST_URI} -f
RewriteRule ^.*$ error/404.php [NC,L]
Any idea ?
Looks like you just need a simple ErrorDocument with use of http:// in target since that causes URL to change in the browser.
Use this line top of your .htaccess:
ErrorDocument 404 /error/404.php
And don't forget to comment out your rewrite rules doing same.
The conditions are tested also on redirects, so you must make sure that the 404 redirect doesn't catch more than it should.
What this means is that %{REQUEST_URI} will contain hidden_folder once it's been redirected, so you must add a RewriteCond to avoid it.
This should work:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/hidden_folder%{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !^/hidden_folder
RewriteRule ^(.*)$ error/404.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ hidden_folder/$1 [L]
Thank you Outlyer.
The actual working code is :
RewriteEngine On
# If the requested file is not in hidden_folder as a file
RewriteCond %{DOCUMENT_ROOT}hidden_folder%{REQUEST_URI} !-f
# AND
# If the requested file not is in hidden_folder as a folder
RewriteCond %{DOCUMENT_ROOT}hidden_folder%{REQUEST_URI} !-d
# AND
# If the requested file is not in root as a file
RewriteCond %{REQUEST_FILENAME} !-f
# AND
# If the requested file is not in root as a folder
RewriteCond %{REQUEST_FILENAME} !-d
# AND
# ???
RewriteCond %{REQUEST_URI} !^/hidden_folder
# THEN Redirect content
RewriteRule ^(.*)$ error/404.php [L]
####################################################
# Enable the deletion of hidden_folder in urls
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ hidden_folder/$1 [L]
I have a url as http://mydomain.com/levels/home?mode=48bb6e862e54f2a795ffc4e541caed4d.
I need to make fake url of above url. When I type a url http://mydomain.com/medium in address bar of browser, request needs to go in url http://mydomain.com/levels/home?mode=48bb6e862e54f2a795ffc4e541caed4d, but address bar will need to show this url http://mydomain.com/medium itself.
How can we do this with .htaccess file?
EDIT:
I tried with
RewriteEngine On
RewriteRule ^medium.*$ /levels/home?mode=48bb6e862e54f2a795ffc4e541caed4d [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
But this will redirect to original url /levels/home?mode=48bb6e862e54f2a795ffc4e541caed4d and showing this url instead of /medium url in address bar.
Use this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule medium.php(.*)$ levels/home?mode=48bb6e862e54f2a795ffc4e541caed4d?$1 [L,QSA]