How to map URL with .htaccess - .htaccess

My local site have project folder http://127.0.0.1/myproject/index.php. I want to be able to map its URL so if someone enters http://127.0.0.1/myproject/service, it should display the same content or perform the same functionality as index.php does.
I tried some example on different site but couldn't get it done.
What I tried so far is :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)\myproject\service$ $index.php

RewriteEngine On
RewriteBase /myproject/
RewriteRule service index.php [L,QSA]
First, setup the base to rewrite. In the rule, all paths are now relative to the base, not absolute.
The flag L denotes no other rules should be applied.
The flag QSA denotes that the query string should be appended.
For more information: http://httpd.apache.org/docs/current/mod/mod_rewrite.html

I would even go as far as to say, check what Wordpress is giving you:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /myproject/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /myproject/index.php [L]
</IfModule>
# END WordPress

Related

.htaccess / mod_rewrite - adding a temporary mandatory string to the URL doesn't take effect

I want to make my website temporarily accessible only at example.com/unfinished-version.
All URLs like /style/main.css or /public/style/main.css would point to /soon.html, then all URLs like /unfinished-version/style/main.css would point to URLs like /public/style/main.css.
The rule I wrote doesn't work, because /public/style/main.css or /public/some-page still works.
RewriteRule ^(?!unfinished-version).*$ soon.html [L,NC]
URLs like example.com/gibberish show soon.html, so it does partly work.
I'll show you full .htaccess, although I find it unnecessary. (The rules shouldn't interfere with this, plus the L flag should avoid processing of any further rules in the first place.)
.htaccess in the root:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(?!unfinished-version).*$ soon.html [L,NC]
RewriteRule ^unfinished-version(.*)$ public/$1 [L,NC]
</IfModule>
.htaccess in public/:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
</IfModule>
P.S.: Not even RewriteRule ^(.*)$ soon.html [L,NC] works.

.htaccess rewrite page with alias

I need to rewrite an url like /services/rental/faq-2/ so that the user (and robots) see /services/rental/faq/
This is my current .htaccess (it's Wordpress) with no successful rewrite:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^/services/rental/faq/?$ /services/rental/faq-2/ [NC,L]
</IfModule>
# END WordPress
Any of the hints I searched actually worked, maybe rule order is important or (most probable) I have no idea on how to do it
Your external URL is /services/rental/faq[/]. This is the one produced by links in your pages, and is the one robots and users see. It is NOT produced by .htaccess -- .htaccess only sees incoming URIs.
Try moving your new RewriteRule up before all the other WordPress RewriteRules (after the RewriteBase).

Cancel rewrite and redirect removing last directory with htaccess

I have a page with this settings:
Options All -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /demo/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !^(.*)show(/|.*)$
RewriteRule . /demo/index.php [L]
</IfModule>
For example, when you request the page http://example.com/demo/any_folder/ it loads http://example.com/demo/index.php without changing the URL, to retrieve data from a database for example. Well, this works.
But I want to add and exception.
I need a condition that if you visit http://example.com/demo/any_folder/show it loads the same URL removing the word "show" but avoiding the redirect that is actually defined. So, it will load http://example.com/demo/any_folder/ and not http://example.com/demo/index.php. Does anyone know how to do this?
There has to be something different about the URL to not be redirected to index.php. The following .htaccess code adds a query parameter ?show to tell the two URLs apart.
Since, the redirection is internal; the user never gets to see the parameter.
RewriteEngine On
RewriteBase /demo/
RewriteRule ^index\.php$ - [L]
RewriteRule ^(.*)/show/?$ $1?show [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{QUERY_STRING} !^show [NC]
RewriteRule . /demo/index.php [L]

.htaccess RewriteRule seems not to work

I got a problem with my .htaccess here and altough I searched the web and tried many things, I could not find any solution...
I have a wordpress-installation with enabled permalinks. Additional to that, I need to rewrite another URL on this wordpress-installation, which does not belong to WP.
If a user browses to http://www.URL.com/?page_id=30&tag=all&filterCategory=6\%23Jackets, I'd like to show http://www.URL.com/jackets.html as URL. There should also be the possibility to directly browse to http://www.URL.com/jackets.html.
My .htaccess looks like this
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^jacket\.html$ http://www.URL.com/?page_id=30&tag=all&filterCategory=6\%23Jackets [L]
</IfModule>
# END WordPress
Does anyone have any idea why this does not work?
The WordPress rewrite rules are redirecting any requests to non-existant files through WordPress. The rewrite rule responsible for this, RewriteRule . /index.php [L], is marked with [L], indicating that no more rules are to be processed after it. In order for your rule to work, it must be placed before the WordPress rules, but after the line RewriteBase /.

Keep url while redirecting to a different page

I want to redirect a particular set of Urls back to the index page of my site, but maintaining their url in the browser address bar. How would I go about to achieve this using mod_rewrite in a htaccess file?
The code currently used:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Custom rewrite below
RewriteRule ^references/(.*) / [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Please note that this is a WP install.
Put this in the htaccess file in your document root, preferably above rules you may already have:
RewriteEngine On
# for each of these "urls" do this
RewriteRule ^/?url_you_want_to_point_to_index / [L]
Assuming you have no query string parameters (the name/value pairs after a ?), that should be all you need.

Resources