Subfolders in .htaccess rewrite rule - for optional title in url - .htaccess

I have two potential paths that could be used:
www.example.com/1/a
www.example.com/1/a/this-is-a-title
I want to serve both urls to the same php file. I need the title to be an optional GET field that can either be supplied or not.
RewriteRule ^1/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ links.php?link=$1&title=$2 [PT,L,QSA]
RewriteRule ^1/([A-Za-z0-9-]+)/?$ links.php?link=$1 [PT,L,QSA]
For some reason the above does not work. It's not serving either of the path options.
What am I getting wrong here?

Related

Rewrite engine not working using htaccess

I am trying to rewrite url using htaccess but it's saying 404.
RewriteEngine on
RewriteRule ^p/([a-z0-9])$ product1.php?pid=$1
It's not opening product1.php file.
You need to make sure your htaccess file and your product1.php file are in same folder. If not that it will give 404 errors.
If they are not present in same folder then you need to complete relative path from the path where your htaccess file is present like: eg: Let's say we have folder structure like:
/root/.htaccess
/root/singh/test/product1.php
So have your rules in following manner then:
RewriteEngine on
RewriteRule ^p/([a-z0-9])$ singh/test/product1.php?pid=$1 [NC,L]
Also apart from these put NC and L flags too in your rules to do case sensitive matching and making this condition's last rule here.

htaccess - redirect a directory including the index page

I am updating a website and having updated a directory name, I need to update all links for the old directory named blog to the newly named press
I have tried this:
RewriteRule ^blog/(.*)$ /press/$1 [R=301,NC,L]
Which works perfect for any url's within that directory. i.e
website-url.com/blog/post-1
Goes too
website-url.com/press/post-1
However the blog index page still doesn't. I instead get a 404 when going to:
website-url.com/blog
If I have a trailing slash, it does work. Just not without.
I know I can use an absolute path redirect like so:
Redirect 301 /blog http://www.website-url.com/press
But the domain could change so I want to keep the path relative / dynamic for this.
Have your rule like this:
RewriteRule ^blog(/.*)?$ /press$1 [R=301,NC,L]
This will match /blog also as /.* part is optional match.

Rewrite url in browser

Hi how do I rewrite URL in .htaccess?
localhost/app/view/login/editPass.php
to
localhost/editPass.php?
when I come from my index
localhost/index.php
I know I have to use, something like
RewriteEngine On
RewriteRule ^(.*)
So do I need to have the .htaccess file with my index or in the folder app?
The .htaccess should be on the parent directory. RewriteRule ^/editPass.php?$ app/view/login/editPass.php
It must have the RewriteEngine On On top first. The "RewriteRule" line is where the magic happens. The line can be broken down into 5 parts:RewriteRule - Tells Apache that this like refers to a single RewriteRule.
^/editPass.php?$
The "pattern". The server will check the URL of every request to the site to see if this pattern matches. If it does, then Apache will swap the URL of the request for the "substitution" section that follows.
app/view/login/editPass.php
The "substitution". If the pattern above matches the request, Apache uses this URL instead of the requested URL.

Htaccess - Detecting the URL

For my family members I was giving each person their own subdomain
(sister1.mydomain.com, sister2.mydomain.com, etc...)
I was using PHP to detect the domain, and then I'd load information related to the subdomain dynamically.
I'd like to get rid of the subdomains and use the power of .htaccess
My goal is to give the same URL:
www.mydomain.com/sister1
www.mydomain.com/sister2
www.mydomain.com/mommy
www.mydomain.com/daddyo
Obviously, I don't plan to have literal working directories for each person.
I'd pass the "sister1" portion to a process.php script that takes care of the rest.
I've figure out how to do it by manually typing each RewriteRule in my htaccess file:
Options +FollowSymLinks
AddDefaultCharset UTF-8
RewriteEngine on
RewriteBase /
RewriteRule ^/?sister1$ process.php?entity=sister1 [L]
RewriteRule ^/?sister2$ process.php?entity=sister2[L]
RewriteRule ^/?mommy$ process.php?entity=mommy[L]
RewriteRule ^/?daddyo$ process.php?entity=daddyo[L]
I feel this is the long way of doing it.
Is there a more universal way of extracting the text after the first "/" forwardslash, and passing it to process.php?entity=$1 ?
I tried it this way:
RewriteRule ^/([A-Za-z0-9-]+)/?$ process.php?entity=$1 [NC,L]
I'm getting the apache 404 error: "Not Found".
It is because you have a mandatory / in the beginning of your rule, i.e., you are always looking for something like /sibling in the URL. Your first examples have that first forward slash as optional due to the question mark after it.
You do not need a beginning forward slash - normally the rewrite rule picks up stuff after the domain name
www.example.com/string/mod/rewrite/gets/is.here
So just remove the starting slash and it should work.

Dynamic URL rewrite with .htaccess

Migrating a site from IIS to Apache and I'm having trouble with a RewriteRule I can't find the answer to.
I need to redirect from www.domain.com/pubs/books/P123.asp to www.domain.com/books.php?p=P123.
No matter what I try, I'm getting a 404.
Can someone give me a pointer? All the examples I seem to find have the dynamic part of the URL at the end. Do I first need to strip the .asp?
Drupal is also running on this site so there is some RewriteRules there to.
The latest I have tried is: RewriteRule /pubs/books/P(.*)$.asp /books.php?p=$1
First of all: Read the documentation! You can find it here.
It says:
Syntax: RewriteRule Pattern Substitution [flags]
and
In VirtualHost context, The Pattern will initially be matched against
the part of the URL after the hostname and port, and before the query
string (e.g. "/app1/index.html").
In Directory and htaccess context,
the Pattern will initially be matched against the filesystem path,
after removing the prefix that led the server to the current
RewriteRule (e.g. "app1/index.html" or "index.html" depending on where
the directives are defined).
If you wish to match against the
hostname, port, or query string, use a RewriteCond with the
%{HTTP_HOST}, %{SERVER_PORT}, or %{QUERY_STRING} variables
respectively.
(in other words: A pattern starting with a slash will never match if in .htaccess context)
First of all, make sure mod_rewrite is enabled and the rewriteengine is on (put RewriteEngine on in your .htaccess) and allowed there (see docs).
Assuming this is in a .htaccess in your www-root, this should work:
RewriteRule ^pubs/books/P(.*)\.asp$ books.php?p=$1 [L]
The syntax ^...$ makes sure that this is the entire filesystem path. We wouldn't want to match http://example.com/some/more/paths/pubs/books/P123.asp here and rewrite that.

Resources