append path directory to url using htaccess - regex

how could i get Apache to redirect
http://localhost/index.php
http://localhost/create/index.php
http://localhost/create/contact.php
http://localhost/engage/page1/services.php
to
http://localhost/Project1/index.php
http://localhost/Project1/create/index.php
http://localhost/Project1/create/contact.php
http://localhost/Project1/engage/page1/services.php
respectively?
Essentially I need to append "Project1" (or whatever other string I see fit) to the BEGINNING of the url path
Thanks

You can use negative lookahead like this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# redirect to /Project1/ if it is not already /Project1/
RewriteRule ^((?!Project1/).*)$ Project1//$1 [L,NC]

Related

Remove parameter name from url using htaccess

I have the following url
http://localhost/mybb/index.php?url=widgetboard/questions&step=1&id=1
which turns into the following url after applying rewrite rule
http://localhost/mybb/widgetboard/questions/step/1/id/1
Is it possible to remove the 2 words ie "step" and "id" from the url and add .html at the very end of the url
my code is
Options -MultiViews
# turn rewriting on
RewriteEngine On
# When using the script within a sub-folder, put this path here, like /mysubfolder/
# If your app is in the root of your web folder, then please delete this line or comment it out
RewriteBase /mybb/
RewriteRule ^(.+?)/(step)/([0-9]+)/(id)/([0-9]+)/?$ index.php?url=$1&$2=$3&$4=$5 [NC,L,QSA]
RewriteRule ^(.+?)/(category)/([0-9]+)/(answer)/([0-9]+)/?$ index.php?url=$1&$2=$3&$4=$5 [NC,L,QSA]
RewriteRule ^(.+?)/(step)/([0-9]+)/?$ index.php?url=$1&$2=$3 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?url=$1&$2=$3 [NC,L,QSA]
You can insert this rule just below RewriteBase /mybb/ line:
RewriteRule ^([^/]+)/([^/]+)/([0-9]+)/([0-9]+)/?$ index.php?url=$1/$2&step=$3&id=$4 [L,QSA]
Change your second rule to:
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?step=$1&id=$2 [NC,L,QSA]
Then if a user clicks this url: domain.com/3/120 your PHP script will receive index.php?step=3&id=120

.htaccess silent redirection to subfolder

I would like to redirect silently all requests of a domain to a subfolder if the folder / file doesn't exist.
Unfortunately some scenarios doesn't work or work non-silently.
works: http://mydomain.com/index.html -- it shows the content of production/index.html
does not work: http://mydomain.com/ -- it doesn't show production/index.html
works: http://mydomain.com/test/ -- it shows the content of production/test/index.html
does not work: http://mydomain.com/test -- it redirects the url (instead of a silent redirection) to http://mydomain.com/production/test/ and shows the content of production/test/index.html
Below is the .htaccess file:
Options -Indexes +SymLinksIfOwnerMatch
DirectoryIndex index.php index.html
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/production/
# Don't apply to URLs that go to existing files or folders
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all those to insert /production folder
RewriteRule ^(.*)$ /production/$1 [L]
It is because mod_dir is appending a trailing slash after mod_rewrite execution. Change your rules to this to overcome this issue:
DirectorySlash Off
Options -Indexes +SymLinksIfOwnerMatch
DirectoryIndex index.php index.html
RewriteEngine on
RewriteBase /
# add a trailing slash for directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{THE_REQUEST} \s/+([^.]*?[^/])[?\s]
RewriteRule [^/]$ /%1/ [L,R=302,NE]
# take care of landing page:
RewriteRule ^/?$ production/ [L]
# Don't apply to URLs that go to existing files or folders
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all those to insert /production folder
RewriteRule ^((?!production).+)$ production/$1 [L,NC]

.htaccess redirection of index.php

The below illustrates the .htaccess code for a generating SEF url when joomla SEF is on in the backend.
## Begin - Joomla! core SEF Section.
#
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#
# If the requested path and file is not /index.php and the request
# has not already been internally rewritten to the index.php script
RewriteCond %{REQUEST_URI} !^/index\.php
# and the request is for something within the component folder,
# or for the site root, or for an extensionless URL, or the
# requested URL ends with one of the listed extensions
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|vcf|raw))$ [NC]
# and the requested path and file doesn't directly match a physical file
RewriteCond %{REQUEST_FILENAME} !-f
# and the requested path and file doesn't directly match a physical folder
RewriteCond %{REQUEST_FILENAME} !-d
# internally rewrite the request to the index.php script
RewriteRule .* index.php [L]
I have a simple problem which I'm not able to get my head around into.
I put this condition so that if the url is domain.com/index.php then it should go to domain.com and this should happen only if the pattern matches exactly as index.php.
However this happens for all urls.
RewriteCond %{THE_REQUEST} [^.]*|/(index.php)$
RewriteRule ^index.php$ / [R=301,L]
it should go to domain.com and this should happen only if the pattern matches exactly as index.php. However this happens for all urls.
It is due to the wrong regex you're using. Use this rule to remove index.php from URIs:
RewriteCond %{REQUEST_URI} !/administrator [NC]
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=302,NC,NE]

.htaccess remove .html file/directory priority rewrite

I'm trying to rewrite so you can access files without .html and without adding a trailing slash. here's the code being used:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.html -f
RewriteRule ^(.*?)/?$ $1.html
But for my file structure on the server where I have this:
/public_html
..
/beginners
beginners.html
and inside /beginners folder there's other files, e.g.
/beginners/page1.html
I need rewrites to work like this:
if user inputs url:
website.com/beginners - the server would return the contents of
file beginners.html currently it returns the contents of the directory /beginners . so i need the server to first check if beginners.html exists, if yes - then server serves beginners.html not the directory /beginners
if the user accesses url website.com/beginners/page1 the server should first check up if
page1.html exists in the folder beginners and if it finds the file page1.html then it serves the contents of the file page1.html
how can this be done?
Here's the .htaccess that does it:
Options +FollowSymLinks -MultiViews -Indexes
DirectorySlash Off
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.html -f
RewriteRule ^(.*?)/?$ $1.html

mod rewrite for pretty urls and also hide index.php

How can i mod-rewrite to obtaining a pretty url like below
example.com
contains a form to get a text value and pass it another file for processing that file is in directory as web but i need to change that as pretty urls
example.com/web/index.php?url=mydomain.com
to
example.com/web/mydomain.com
example.com/web/newdomain.com
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+web/index\.php\?url=([^\s&]+) [NC]
RewriteRule ^ /web/%1? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^web/(.+?)/?$ /web/index.php?url=$1 [L,QSA]